简体   繁体   中英

Connecting to Embedded Bluetooth Device From Android Device

i just started working on Connecting to Embedded BT Device through My Android Phone.It is connecting fine but i am facing problem when i am not disconnecting it properly. I mean first close socket and then any i/o steams opened.

But when i turn off my Bluetooth suddenly in device how will i know the bluetooth is get disconnecting. is there is any way to receive Bluetooth disconnecting listener all the time in the APP.

Any Ideas ....? Thanks

mmSocket= device.createRfcommSocketToServiceRecord(uuidToTry);
try
{
    mmSocket.connect();
}
catch (IOException e)
{
    mmSocket.close();
}

You can do it like this- Create a broadcast receiver in your code Like this:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (!mBluetoothAdapter.isEnabled()) {
                // BT is turened off.
            }
            else{
                // BT is turened on.
              }
        }
 }
};

and register that broadcastreceiver for following intent filter:

IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(mReceiver, filter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM