我想从串口蓝牙设备接收数据到android手机.但我不知道该设备的UUID如何找到该设备的UUID?
我需要一个蓝牙设备的UUID连接到它,但我不太了解uuid.它是否特定于每个设备以及如何知道什么是device's uuid.可以使用代码完成吗?
好的,所以我有一个bluetooth.java类(我希望)会发送我的字符串..但是每当我尝试发送命令时,我的破碎的吐司只是显示说不.我错过了重要的事情吗?我只有这两个功能..
void openBT() throws IOException {
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
try {
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Unable to create socket", Toast.LENGTH_LONG).show();
}
try {
mmSocket.connect();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Unable to connect to socket", Toast.LENGTH_LONG).show();
}
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
}
void sendData2() throws IOException {
if (BtCommand == "0"){
msg = "Fan2 Off";
}
if (BtCommand == "1"){
msg = "Fan2 On";
}
mmOutputStream.write(msg.getBytes());
}
Run Code Online (Sandbox Code Playgroud)
这些是我设定的唯一内容
BluetoothSocket mmSocket;
BluetoothDevice mmDevice; …Run Code Online (Sandbox Code Playgroud) 我想连接到此设备:http://www.rhydolabz.com/index.php?main_page = product_info&product_id = 479
我尝试使用BluetoothChat样本中的类.但它没有用.它说unable to connect device.
然后我尝试在try catch中使用以下代码
// Create a Socket connection: need the server's UUID number
Method m = d.getClass().getMethod("createRfcommSocketToServiceRecord", new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(d, 1);
socket.connect();
Log.d("WCAM", ">>Client connectted");
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
socket.close();
Toast.makeText(this, "Bluetooth is Connected", Toast.LENGTH_LONG).show();
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
04-18 20:45:01.660: W/System.err(6780): java.lang.NoSuchMethodException: createRfcommSocketToServiceRecord [int]
04-18 20:45:01.660: W/System.err(6780): at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-18 20:45:01.660: W/System.err(6780): at java.lang.Class.getMethod(Class.java:915)
04-18 20:45:01.660: W/System.err(6780): at com.xpleria.wirelesscontroller.Login.onStart(Login.java:90) …Run Code Online (Sandbox Code Playgroud)