如何修复Java中的“没有BluetoothManagerCallback调用的getBluetoothService()”错误?

时间:2019-03-27 09:49:10

标签: java android bluetooth

我正在编写用于通过蓝牙将android应用程序连接到Arduino的基本代码。 连接已绑定,但当Socket.run();启动它会在IDE(android studio)的运行终端上释放错误

有人遇到这个问题吗?

我试图更改UUID,更改UUID的初始化并调试代码,但没有一个解决问题

MainActivity.java

// Pair button pressed 
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //first cancel discovery because it's very memory intensive.
        Bluetooth_Adapter.cancelDiscovery();

        Log.d(TAG, "onItemClick: You Clicked on a device.");
        String deviceName = Devices_Array.get(i).getName();
        String deviceAddress = Devices_Array.get(i).getAddress();

        Log.d(TAG, "onItemClick: deviceName = " + deviceName);
        Log.d(TAG, "onItemClick: deviceAddress = " + deviceAddress);

        //create the bond.
        //NOTE: Requires API 17+?
        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2){
            Log.d(TAG, "Trying to pair with " + deviceName);
            Devices_Array.get(i).createBond();
// I'm getting the device from a list view
            BTDevice = Devices_Array.get(i);
            ConnectionService = new BluetoothConnectionService(MainActivity.this);
        }
    }

BluetoothConnectionService.java

        public void run(){
            byte[] buffer = new byte[1024];  // buffer store for the stream

            int bytes; // bytes returned from read()

            // Keep listening to the InputStream until an exception occurs
            while (true) {
                // Read from the InputStream
                try {
                    bytes = InStream.read(buffer);
                    String IncomingMessage = new String(buffer, 0, bytes);
                    Log.d(TAG, "InputStream: " + IncomingMessage);
                    Result.setText(IncomingMessage);
                } catch (IOException e) {
                    Log.e(TAG, "write: Error reading Input Stream. " + e.getMessage() );
                    break; //End the connection if smth happened
                }
            }
        }

有人可以帮我吗 我尝试了3天,以修复该错误,但它始终显示以下消息。 我试图更改UUID,更改初始化和调试代码,但是都没有解决问题

  
      
  • D / BTConnectionService:ConnectThread:尝试创建RfCOM套接字   使用UUID:80c44f06-1ee0-466a-b584-77de65cf3cf7
  •   
  • BluetoothAdapter:getBluetoothService()调用时不带   BluetoothManagerCallback   -D / BTConnectionService:运行:关闭的套接字。
  •   
  • 运行:ConnectThread:无法连接到UUID:80c44f06-1ee0-466a-b584-77de65cf3cf7
  •   
  • D / BTConnectionService:已连接:
  •   
  • 开始。 D / BTConnectionService:ConnectedThread:正在启动。
  •   
  • E / BTConnectionService:写入:读取输入流时出错。插座   已关闭
  •   

有人遇到这个问题吗?

0 个答案:

没有答案