代码之家  ›  专栏  ›  技术社区  ›  Dinesh Hebbar

即使配对,也无法从设备接收数据,出现IO异常错误。在没有BluetoothManagerCallback的情况下调用getBluetoothService

  •  0
  • Dinesh Hebbar  · 技术社区  · 7 年前

    我已经构建了一个android应用程序来接收来自设备的数据,这是我第一次获得所有的值,但如果我第二次尝试数据没有接收到,我必须将其从配对列表中删除,并再次配对,在android studio中,我收到IOException错误和“getBluetoothService调用时没有BluetoothManagerCallback”。

    在某些设备中工作正常。

    enter image description here

     @Override
        protected void onResume() {
            super.onResume();
    
            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (mBluetoothAdapter != null) {
                if (!mBluetoothAdapter.isEnabled())
                {
    
                    Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
                } else {
    
                    scanAndPairDevice();
                }
    
            } else {
                // TODO Android Device doesnt Support Bluetooth
            }
        }
    
    private void scanAndPairDevice() {
            if (isDevicePaired()) {
                receiveDataFromDevice();
    
            } else {
    
                if(flag!=1) {
    
                    showAlertDialog(mContext, "SPLTECH Device is not paired!", "Please pair the device to connect", 1);
                }
                else {
    
                    showAlertDialog(mContext, "SPLTECH Device is not registered!", "Please conect with a registered device", 3);
                }
                //getDeviceListFromServer();
            }
        }
    
    private boolean isDevicePaired() {
    
            Set<BluetoothDevice> bluetoothDeviceSet = mBluetoothAdapter.getBondedDevices();
            if (bluetoothDeviceSet.size() > 0) {
    
                Set<String> set=mSesssion.getDeviceList();
                Iterator<BluetoothDevice> iT=bluetoothDeviceSet.iterator();
                while(iT.hasNext()){
    
                    BluetoothDevice device=iT.next();
                    for(String device1: set)
                    {
                        if(device.getName().equals("SPLTECH"))
                        {
                            flag=1;
                        }
    
                        if(device.getAddress().equals(device1))
                        {
    
                            mDevice = device;
                            mSesssion.setDeviceMacId(device.getAddress());
                            return true;
                        }
    
                    }
    
                }
            }
            return false;
        }
    
    
    private void receiveDataFromDevice() {
    
            mProgressBar.setVisibility(View.GONE);
            mHint.setVisibility(View.VISIBLE);
            Toast.makeText(mContext, "Now,Bluetooth is Ready to Take Value From Device..!", Toast.LENGTH_SHORT).show();
    
            myThreadConnectBTdevice = new ThreadConnectBTdevice();
            myThreadConnectBTdevice.start();
        }
    
    
     private class ThreadConnectBTdevice extends Thread {
    
            private BluetoothSocket bluetoothSocket = null;
    
            private ThreadConnectBTdevice() {
    
                try {
                    Log.i("hii","hello");
                    //bluetoothSocket = mDevice.createRfcommSocketToServiceRecord(myUUID);
                    Method m = mDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
                    bluetoothSocket = (BluetoothSocket) m.invoke(mDevice, 1);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Log.i("hii","hello");
                    e.printStackTrace();
    
                }
            }
    
            @Override
            public void run() {
                boolean success = false;
                try {
                    bluetoothSocket.connect();
                    success = true;
                } catch (IOException e)
                {
                    e.printStackTrace();
    
                    final String eMessage = e.getMessage();
                    runOnUiThread(new Runnable() {
    
                        @Override
                        public void run() {
    
                            // textStatus.setText("something wrong bluetoothSocket.connect(): \n" + eMessage);
                        }
                    });
    
    
                   try{
                        bluetoothSocket.close();
    
                    }
                    catch (IOException ex)
                    {
    
                    }
    
                }
    
                if (success) {
                    //connect successful
                    startThreadConnected(bluetoothSocket);
    
                } else {
                    //fail
                    Log.i("hii","in false");
    
    
                }
            }
    
            public void cancel() {
    
                try {
                    bluetoothSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            }
    
        }
    
    
    private void startThreadConnected(BluetoothSocket socket) {
    
            myThreadConnected = new ThreadConnected(socket);
            myThreadConnected.start();
        }
    
        /*
        ThreadConnected:
        Background Thread to handle Bluetooth data communication
        after connected
         */
        private class ThreadConnected extends Thread {
            private final BluetoothSocket connectedBluetoothSocket;
            private final InputStream connectedInputStream;
            private final OutputStream connectedOutputStream;
    
    
            public ThreadConnected(BluetoothSocket socket) {
    
                connectedBluetoothSocket = socket;
                InputStream in = null;
                OutputStream out = null;
    
                try {
                    in = socket.getInputStream();
                    out = socket.getOutputStream();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                connectedInputStream = in;
                connectedOutputStream = out;
            }
    
            @Override
            public void run() {
                byte[] buffer = new byte[1024];
                int bytes;
    
    
                while (true) {
                    try {
    
                        bytes = connectedInputStream.read(buffer);
                        final String strReceived = new String(buffer, 0, bytes);
    
                        Log.d("abc", strReceived);
    
                        Message readMsg = mHandler.obtainMessage(
                                DATA_FROM_DEVICE, strReceived);
                        readMsg.sendToTarget();
    
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
    
    
                        final String msgConnectionLost = "Connection lost:\n"
                                + e.getMessage();
    
                        runOnUiThread(new Runnable() {
    
                            @Override
                            public void run() {
                                mHint.setText(msgConnectionLost);
                            }
                        });
    
                        break;
                    }
                }
    
            }
    
            public void write(byte[] buffer) {
                try {
                    connectedOutputStream.write(buffer);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    
            public void cancel() {
                try {
                    connectedBluetoothSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
    0 回复  |  直到 7 年前