蓝牙插座警告

时间:2018-06-19 14:50:29

标签: android android-bluetooth

public class Bluetooth_activity extends AppCompatActivity {
BluetoothSocket socket;
String uuid = "fa87c0d0-afac-11de-8a39-0800200c9a66";
public OutputStream outputStream;
BluetoothAdapter mbluetoothaadapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth_activity);
    mbluetoothaadapter= BluetoothAdapter.getDefaultAdapter();
    final BluetoothController mBTController = BluetoothController.getInstance().build(Bluetooth_activity.this);

    mBTController.setBluetoothListener(new BluetoothListener() {


        @Override
        public void onActionStateChanged(int preState, int state) {

        }

        @Override
        public void onActionDiscoveryStateChanged(String discoveryState) {

        }

        @Override
        public void onActionScanModeChanged(int preScanMode, int scanMode) {

        }

        @Override
        public void onBluetoothServiceStateChanged(int state) {

        }

        @Override
        public void onActionDeviceFound(BluetoothDevice device, short rssi) {
            BluetoothDevice dispositivo = mbluetoothaadapter.getRemoteDevice(device.getAddress());

            try {
                device.createBond();
                socket = dispositivo.createInsecureRfcommSocketToServiceRecord(UUID.fromString(uuid));//create a RFCOMM (SPP) connection
                socket.connect();
                Log.i("state", "connnected");
                outputStream = socket.getOutputStream();

                Toast.makeText(getApplicationContext(),
                        "Connection to bluetooth device successful", Toast.LENGTH_LONG).show();



            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onReadData(BluetoothDevice device, byte[] data) {

        }
    });
    Log.i("soc",""+socket);
    mBTController.startScan();
}
}

**我正在尝试通过蓝牙使用自己的android应用程序连接到android设备,我收到此警告(图中所示),这不允许我连接到任何设备。我搜索了为什么会出现警告我发现这是因为我试图一次打开两个插槽,所以我试图关闭所有打开的插槽,但同时也提示没有打开的插槽。请帮助我找出此问题

提前感谢**

1 个答案:

答案 0 :(得分:0)

// This method is called back when a new device is found, it is where you get any
// information from the BluetoothDevice, you can keep this information in a list
// which shows all the BluetoothDevices that were found
mBTController.setBluetoothListener(new BluetoothListener() {

    @Override
        public void onActionDeviceFound(BluetoothDevice device, short rssi) {
        // Your problem is you are connecting every device when it is found
    }
    ....
}

// Pick up a BluetoothDevice from the list and call connectDevice(selectedDevice)

// This is where you connect to the desired BluetoothDevice
void connectDevice(BluetoothDevice myDevice)
{
    device.createBond();
    socket = dispositivo.createInsecureRfcommSocketToServiceRecord(
                UUID.fromString(uuid));//create a RFCOMM (SPP) connection
    socket.connect();
    Log.i("state", "connnected");
    outputStream = socket.getOutputStream();

    Toast.makeText(getApplicationContext(),
          "Connection to bluetooth device successful", Toast.LENGTH_LONG).show();
}