我正在尝试与蓝牙可编程微控制器进行通信。微控制器上的蓝牙设备(具体地)在蓝牙串行COM端口4上进行通信。
问题:如何让Android App从此COM端口读取数据(编号4)?
我知道UUID是一个众所周知的唯一ID,适用于此设备,但我不认为它与指定COM端口有任何关系。
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
btSocket = btDevice.createRfcommSocketToServiceRecord( myUUID);
btSocket.connect();
valid.append( btDevice.getName() + "\n" + btDevice.getAddress());
north.append("Socket Connected");
InputStream mmInStream = btSocket.getInputStream();
OutputStream mmOutStream = btSocket.getOutputStream();
byte[] buffer = new byte[10];
int bytes;
StringBuffer str = new StringBuffer();
while (true) {
try {
mmOutStream.write("a".getBytes());
//Reads a # of bytes until the end of stream is reached
bytes = mmInStream.read(buffer);
//Transform to string
str.append(buffer.toString()+"\t"); //Clear the buffer
Log.e("DATA", "THE DATA: "+ str.toString());
south.setText(str.toString());
str.delete(0,str.length());
} catch (IOException e) {
break;
} }}
答案 0 :(得分:3)
COM端口仅存在于微控制器上,而不是连接到它的蓝牙设备。蓝牙设备甚至不知道微控制器用来连接它的COM端口。蓝牙设备与micro的连接是通过TX和RX线路。它们连接到分配给特定COM端口的微型引脚上的事实与蓝牙设备无关,并且是未知的。
答案 1 :(得分:1)
我使用自己制作的自定义蓝牙设备遇到了这个问题。而不是在您的连接线程中使用createRfcommSocketToServiceRecord,尝试类似于以下内容:
public ConnectThread(BluetoothDevice device) throws
SecurityException, NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
mmDevice = device;
BluetoothSocket tmp = null;
// Force a BluetoothSocket for a connection with the
// given BluetoothDevice
Method m = mmDevice.getClass().getMethod("createRfcommSocket",
new Class[]{int.class});
mmSocket = (BluetoothSocket)m.invoke(mmDevice, Integer.valueOf(1));
}
我的mmDevice就是你的btDevice。
这会强制未知设备和智能手机之间的套接字连接。据我所知,Android连接“非相似”设备存在问题。值得一试。