When there are no matching devices and no available devices are found.
//If There is paired devices, add each one to the Arrayadapter if(Paireddevices.size () > 0) {Findviewbyid (r.id.title_paired_devices). setvisibility (view.visible); for(Bluetoothdevice device:paireddevices) {Mpaireddevicesarrayadapter.add (Device.getname ()+ "\ n" +device.getaddress ()); } } Else{ String nodevices = getresources (). GetText (r.string.none_paired). toString (); Mpaireddevicesarrayadapter.add (nodevices); }
//When Discovery finds a device if(BluetoothDevice.ACTION_FOUND.equals (ACTION)) {//Get The Bluetoothdevice object from the IntentBluetoothdevice device =Intent.getparcelableextra (Bluetoothdevice.extra_device); //If It's already paired, skip it, because it ' s been listed already if(Device.getbondstate ()! =bluetoothdevice.bond_bonded) {Mnewdevicesarrayadapter.add (Device.getname ()+ "\ n" +device.getaddress ()); } //When discovery are finished, change the Activity title}Else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals (ACTION)) {setprogressbarindeterminatevisibility ( c8>false); Settitle (R.string.select_device); if(Mnewdevicesarrayadapter.getcount () = = 0) { String nodevices = getresources (). GetText (R.string.none_found). toString (); Mnewdevicesarrayadapter.add (nodevices); } }
The two strings shown here. However, there is a click event that Fu Hui the following 17 characters of the string, resulting in an error.
//The On-click listener for all devices in the ListViews PrivateOnitemclicklistener Mdeviceclicklistener =NewOnitemclicklistener () { Public voidOnitemclick (adapterview<?> av, View V,intArg2,LongArg3) { //Cancel Discovery because it ' s costly and we ' re about to connectMbtadapter.canceldiscovery (); //Get The device MAC address, which is the last chars in the View String info = ((TextView) v). GetText ( ). ToString (); String address = info.substring (Info.length ()-17); //Create The result Intent and include the MAC addressIntent Intent =NewIntent (); Intent.putextra (extra_device_address, ADDRESS); //Set result and finish this ActivitySetresult (ACTIVITY.RESULT_OK, intent); Finish (); }
Go back to the last Aty deal:
Private void Boolean secure) { // Get The device MAC address String address = Data.getextras (). getString (devicelistactivity.extra_device_address); Get the Bluetoothdevice object bluetoothdevice device = mbluetoothadapter.getremotedevice (address); // attempt to connect to the device mchatservice.connect (device, secure); }
Obviously, if it is displayed "no matching string found" or "Device not found", it is not a valid MAC address.
Solution: Just add another layer of judgment.
Bluetooth Chat sample bug in the website of Bluetooth development instance