Answer the question
In order to leave comments, you need to log in
android scan bluetooth how to do reviciever?
I requested the following rights:
uses-permission android:name="android.permission.BLUETOOTH" />
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
public void onCreate(Bundle savedInstanceState) {
bt3 = (Button) findViewById(R.id.button3);
bt3.setOnClickListener(btnScanDeviceOnClickListener);
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(bReceiver, filter);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
private Button.OnClickListener btnScanDeviceOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
adapter.startDiscovery();
tv2.setText("scanning...2");
}
};
private final BroadcastReceiver bReciever = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
tv1.setText("scanning...2");
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
tv1.setText("* "+device.getAddress()+" | "+device.getName()+"\n");
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action) && !adapter.isEnabled()) {
tv1.setText("finish");
}
else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
tv1.setText("start");
}
}
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question