Android 蓝牙连接另一个蓝牙设备 [英] Android Bluetooth to connect another bluetooth device
问题描述
我正在做基于蓝牙的应用程序,我想连接其他设备,如诺基亚设备和打印机.
Im doing bluetooth based application, I want to connect other devices like nokia devices, and printer.
我参考了android蓝牙文档http://developer.android.com/guide/topics/connectivity/bluetooth.html.它演示了所有基本的蓝牙 API 功能,我做了所有这些事情
I refer the android bluetooth documentation http://developer.android.com/guide/topics/connectivity/bluetooth.html. It demonstrates all the fundamental Bluetooth API capabilites, And I did these all the things
Scanning for other Bluetooth devices
Querying the local Bluetooth adapter for paired Bluetooth devices
Establishing RFCOMM channels/sockets
Connecting to a remote device
Transfering data over Bluetooth
我从BluetoothChat获得了参考,android的示例.
i get reference from BluetoothChat, samples of android.
BluetoothChat This application send data to another android device but for that this application must be installed in both the devices.
像这样如何通过代码通过蓝牙将文件从Android设备发送到其他设备
我想要的是
- 我想将文件从我的应用程序从一台设备发送到另一台设备,即使另一台未运行我们的应用程序的设备也可以使用.即接收器设备也可以使用默认蓝牙接收文件.
这在安卓中可行吗?
推荐答案
您可以轻松地在两个 BT 设备之间建立连接.你只需要打电话
You can do connection between two BT devices easily. You just need to call
createRfcommSocketToServiceRecord(UUID)
使用能够理解接收器设备的 UUID.对于文件传输操作,UUID 必须等于(例如)00001106-0000-1000-8000-00805F9B34FB(文件传输服务)
with UUID that understand receiver device. For file transfer action UUID must be equal (for example) to 00001106-0000-1000-8000-00805F9B34FB (File transfer service)
所以你的连接代码可能看起来像下面的代码
So you connection code might looks like code below
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("00:0A:94:16:77:A0");BluetoothSocket clientSocket;
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("00:0A:94:16:77:A0"); BluetoothSocket clientSocket;
try {
log(TAG, "Remote device " + device);
ParcelUuid[] uuids = device.getUuids();
boolean isFileTransferSupported = false;
UUID ftpUID = UUID.fromString("00001106-0000-1000-8000-00805F9B34FB");
// Check if remote device supports file transfer
for (ParcelUuid parcelUuid: uuids) {
if (parcelUuid.getUuid().equals(ftpUID)) {
isFileTransferSupported = true;
break;
}
}
if (!isFileTransferSupported) {
log(TAG, "Remote bluetooth device does not supports file transfer ");
return;
}
clientSocket = device.createRfcommSocketToServiceRecord(ftpUID);
clientSocket.connect();
} catch (IOException e) {
return;
}
这篇关于Android 蓝牙连接另一个蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!