And*_*ing 5 android android-bluetooth bluetooth-socket
我们的设备通过蓝牙发送数据,在Android应用程序中我们需要读取这些数据.
我能够建立蓝牙连接,接下来我正在调用一个Thread来使用BluetoothDevice建立BluetoothSocket连接.在读取字节时,它返回0(零)此外,while循环仅运行一次.
我在下面的代码中使用的UUID也来自某些蓝牙代码段.我是否需要获取设备的正确UUID.
请有人帮忙吗?.如果你给我一个有用的答案,我将非常感谢.
//Calling ConnectThread after Bluetooth is paired
public class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private final UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-00805f9b34fb");
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
String name = device.getName();
Log.e("Device Name ", name);
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mmSocket = tmp;
}
public void run() {
// mAdapter.cancelDiscovery();
try {
mmSocket.connect();
ConnectedThread mConnectedThread = new ConnectedThread(mmSocket);
mConnectedThread.start();
} catch (IOException connectException) {
// try {
// mSocket.close();
// } catch (IOException closeException) { }
return;
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024];
int begin = 0;
int bytes = 0;
while (true) {
try {
// bytes += mmInStream.read(buffer, bytes, buffer.length
// - bytes);
if (mmSocket.isConnected()) {
Log.e("Socket is connected", "Socket is connected");
}
int numOfBytes = mmInStream.available();
Log.e("numOfBytes", String.valueOf(+numOfBytes));
bytes = mmInStream.read(buffer);
// mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
// .sendToTarget();
} catch (IOException e) {
break;
}
}
}
i am struck and unable to read the data from the Bluetooth
Run Code Online (Sandbox Code Playgroud)
我是否需要获取设备的正确 UUID?
是的。您可以使用getUuids()返回BluetoothDevice远程设备支持的功能 (UUID) 的命令,或者fetchUuidsWithSdp()如果需要新的 UUID。
while 循环仅运行一次。
这是因为您的代码抛出错误并且您正在处理此错误并退出循环
while (true) {
try {
// bytes += mmInStream.read(buffer, bytes, buffer.length
// - bytes);
if (mmSocket.isConnected()) {
Log.e("Socket is connected", "Socket is connected");
}
int numOfBytes = mmInStream.available();
Log.e("numOfBytes", String.valueOf(+numOfBytes));
bytes = mmInStream.read(buffer);
// mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
// .sendToTarget();
} catch (IOException e) {
// THIS BREAK INTERRUPT YOUR WHILE-LOOP SENTENCE
break;
}
}
Run Code Online (Sandbox Code Playgroud)
这里,当读取字节时,它返回 0
因为您创建了套接字的 InputStream 它没有连接到任何东西(因为您创建的用于生成套接字的 UUID 在您进行蓝牙连接的设备中不存在)
编辑
获取 uuid 的其他方法是配置广播接收器以获取 uuid:
//Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_UUID);
//Set your other filters
registerReceiver(ActionFoundReceiver, filter); // Don't forget to unregister during onDestroy
Run Code Online (Sandbox Code Playgroud)
并创建 BroadcastRecievier 以获取 UUID:
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_UUID.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
for (int i=0; i<uuidExtra.length; i++) {
Log.i("TEST-UUIDS","Device: " + device.getName() + ", " + device + ", Service: " + uuidExtra[i].toString());
}
}
else if(BluetoothDevice.YOUR_OTHER_ACTION_1){
//YOUR CODE FOR ACTION 1
}
else if(BluetoothDevice.YOUR_OTHER_ACTION_2){
//YOUR CODE FOR ACTION 2
}
}
};
Run Code Online (Sandbox Code Playgroud)
注意:并非每个蓝牙设备都会显示其服务 uuid
| 归档时间: |
|
| 查看次数: |
811 次 |
| 最近记录: |