【问题标题】:Sending bluetooth strings in android在android中发送蓝牙字符串
【发布时间】:2016-01-21 10:48:26
【问题描述】:

好的,所以我有一个 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());
}

这些是我唯一设置的东西

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

我确定是 openBT 功能失败,我已与设备配对。

【问题讨论】:

    标签: java android bluetooth


    【解决方案1】:

    您确定选择了 UUID 吗?

    List of UUIDs

    或者,尝试重新编写您的代码。我使用 2 个线程(1 个用于连接,第二个用于连接和通信)

    【讨论】:

    • 我不确定不是吗?我以为这个 uuid 对应所有蓝牙设备,当我尝试时,我在 2 部安卓手机(SONY Z1 和 SONY Z5)之间尝试了
    • 我正在使用 00001112-0000-1000-8000-00805f9b34fb
    • 它仍然无法正常工作.. 不知道为什么,我什至尝试了一些随机的蓝牙终端应用程序,它们甚至没有连接
    • 尝试在另一部手机上测试(可能是硬件或设置问题)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 2014-05-18
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    相关资源
    最近更新 更多