F
F
Fisab2017-03-22 01:22:21
Java
Fisab, 2017-03-22 01:22:21

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" />

On the button, I start the scan and then check:

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

I can not understand why it does not find any device.
After starting, "start" immediately pops up, a little later tv1 changes to "scanning2 ...", but nothing else happens.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question