I made an app by this code:
// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String str = device.getName() + "\n" + device.getAddress();
bluFoundArrayAdapter.add(str);
}
}
};
public void searchBluetooth(View v) {
// Register for broadcasts when a device is discovered.
if(myBluetoothAdapter.isDiscovering()) {
myBluetoothAdapter.cancelDiscovery();
} else {
Intent intent = new Intent(BluetoothDevice.ACTION_FOUND);
sendBroadcast(intent);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
myBluetoothAdapter.startDiscovery();
}
}
my expect is when I press the button "search for Bluetooth device", then Bluetooth devices within range would be shown in the listview, but the App stopped when I pressed the button, and in the logout, it was these:
Whats more, I have added the 2 permissions into AndroidMenifest.xml:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />