Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to Embedded Bluetooth Device From Android Device

Tags:

android

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();
}
like image 659
praveen Avatar asked Feb 27 '13 09:02

praveen


People also ask

Why is Bluetooth not finding devices?

It's usually because the device's own Bluetooth is switched off, or isn't in pairing mode. Check the user manual to find out how to make it discoverable by turning on pairing mode. With some headphones, it's a case of holding the power button down for longer, but with other devices there's a dedicated Bluetooth button.

How do I find a hidden Bluetooth device?

If you want a more reliable way to find hidden Bluetooth devices, you can use a Bluetooth checker app. These apps will scan for any hidden devices that are connected to your computer or phone. There are a lot of different Bluetooth checker apps available.

How do you integrate Bluetooth on Android?

In order to enable the Bluetooth of your device, call the intent with the following Bluetooth constant ACTION_REQUEST_ENABLE. Its syntax is. Intent turnOn = new Intent(BluetoothAdapter. ACTION_REQUEST_ENABLE); startActivityForResult(turnOn, 0);


1 Answers

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);

like image 78
Shridutt Kothari Avatar answered Sep 18 '22 13:09

Shridutt Kothari