简体   繁体   English

在android中发送蓝牙字符串

[英]Sending bluetooth strings in android

Ok, so I have a bluetooth.java class which (i'd hoped) would send my strings.. however whenever I try to send the command my broken toasts just show saying nope. 好的,所以我有一个bluetooth.java类(我希望)会发送我的字符串..但是每当我尝试发送命令时,我的破碎的吐司只是显示说不。 Am I missing something vital? 我错过了重要的事情吗? I only have these 2 functions in there.. 我只有这两个功能..

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());
}

and these are the only things I set 这些是我设定的唯一内容

  BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
String BtCommand;
OutputStream mmOutputStream;
InputStream mmInputStream

I am sure it is the openBT function which is failing, I am paired to a device fine. 我确信这是openBT功能失败,我配对设备很好。

Are you sure with selected UUID? 你确定选择了UUID吗?

List of UUIDs UUID列表

Alternatively, try to rework your code. 或者,尝试重新编写代码。 I use 2 threads (1 for connect and second connected and communication) 我使用2个线程(1个用于连接,第二个连接和通信)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM