Bluetooth Chat sample bug on the website of the development example

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.