使用BluetoothSock通过android蓝牙接收字符串

使用BluetoothSock通过android蓝牙接收字符串,android,android-studio,android-bluetooth,Android,Android Studio,Android Bluetooth,此代码接收从嵌入式蓝牙到Android手机的字符串。我希望当嵌入式蓝牙传输启动时,此功能能够工作,当嵌入式蓝牙向手机发送“A”时,将文本视图的背景更改为黑色,当它发送“Z”时,将背景更改为绿色。此代码不起作用,当按下按钮后调用此函数时,应用程序将关闭。有什么问题吗 public void run() { byte[] buffer = new byte[1024]; while (true) { try { x= btSocket.ge

此代码接收从嵌入式蓝牙到Android手机的字符串。我希望当嵌入式蓝牙传输启动时,此功能能够工作,当嵌入式蓝牙向手机发送“A”时,将文本视图的背景更改为黑色,当它发送“Z”时,将背景更改为绿色。此代码不起作用,当按下按钮后调用此函数时,应用程序将关闭。有什么问题吗

public void run() {

    byte[] buffer = new byte[1024];

    while (true) {
        try {
            x= btSocket.getInputStream().read( buffer);

            if(x==65)
                tb.setBackgroundColor(Color.BLACK);
            if(x==90)
                tb.setBackgroundColor(Color.GREEN);


        } catch (IOException e) {
            break;
        }

    }

}

首先,应用程序可能正在崩溃,而不仅仅是关闭。你应该检查一下你的日志。首先,应用程序可能正在崩溃,而不仅仅是关闭。你应该检查一下你的日志。