Question Detail

I want to show Bluetooth icon in my App if Bluetooth is connected to any device. How can i fine programically is android device Mobile is connected to bluetooth?


Thread Reply

Hemant Sharma

- 6 months ago

To grante Bluetooth Permission you have to set it both side In manifest and In your java file for Runtime Bluetooth Permission 

Manifest.xml

<uses-permission android:name="android.permission.BLUETOOTH"/>

 

In your Java file

 if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH}, 465);
}

 

void checkbluetoothIsConnectMethod() {
    //The BroadcastReceiver that listens for bluetooth broadcasts
    mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
                bluetooth_status_LinearLayout.setVisibility(View.VISIBLE);
                bluetooth_status_textVIew.setText(device.getName().toString() + " Connected");
            } else {
                bluetooth_status_LinearLayout.setVisibility(View.GONE);
            }
        }
    };
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mReceiver, filter);
}

Anonymous

- 6 months ago

Where does it shows?

Hemant Sharma

- 6 months ago

I have created layout for Bluetooth Connection status

when you found that your bluetooth is connected visible or change Text accordingly 

ACTION_ACL_CONNECTED this action will tell you that you are connected or not there more status like

 if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
          
            Log.v(TAG, "connected: " + device);
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
          
        Log.v(TAG, "disconnected: " + device);
        }
        else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
          
        Log.v(TAG, "found: " + device + "");
        }
        else if (blueAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            
            Log.v(TAG, "Discovery started");
        }
        else if (blueAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
        
            Log.v(TAG, "Discovery finished");
        }

 

if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
                bluetooth_status_LinearLayout.setVisibility(View.VISIBLE);
                bluetooth_status_textVIew.setText(device.getName().toString() + " Connected");
            } else {
                bluetooth_status_LinearLayout.setVisibility(View.GONE);
            }