This article for Bo Master Original, reproduced please indicate the source: http://blog.csdn.net/zrf1335348191/article/details/50995466
Bluetooth related code has been described in another two articles, you need to be able to view
android4.42-settings Bluetooth module for source code Analysis Bluetooth (UP)
android4.42-setting Bluetooth module for source code Analysis Bluetooth (bottom)
one,switch bluetooth Switch
Switch from create to action state listening steps such as the following
Switch Actionbarswitch = new switch (activity);
- Adding an instance to Actionbar
Activity.getactionbar (). Setcustomview (.....).;
- Passing a switch instance to a Bluetoothenabler instance by constructing a method
Mbluetoothenabler = new Bluetoothenabler (activity, actionbarswitch);
- How to invoke the join menu in fragment
Sethasoptionsmenu (TRUE);
- Call the Resume method on an instance of Bluetoothenabler in the Onresume method
The above series of code is completed in the Bluetoothsettings.java, followed by processing in the Bluetoothenabler.java
- To infer whether Bluetooth is available or not, set switch to non-clickable
- Update switch status based on local Bluetooth status
- Register to filter Bluetoothadapter.action_state_change broadcasts. Update the switch state when Bluetooth status is changed
- Add a listener event for switch, change the local Bluetooth adapter, update the switch state after the local Bluetooth adapter has changed
To summarize, switch-related logic implementations are these. Create a switch instance in Bluetoothsettings, In Bluetoothenabler.java the status of switch is monitored and updated, it is not difficult to see the code in the Bluetoothenabler.java class that is specifically for switch processing.
Both . Local Bluetooth related
- Create a local Bluetooth preference
Mmydevicepreference = new Preference (getactivity ());
Preferencescreen.addpreference (mmydevicepreference);
- Constructs an instance of Bluetoothdiscoverableenabler to display updates to Mmydevicepreference's subtitle summary
Mdiscoverableenabler = new Bluetoothdiscoverableenabler (getactivity (), mlocaladapter, mmydevicepreference); Mdiscoverableenabler.resume ();
The above code is completed in Bluetoothsettings. Preference contains title--bluetooth name, summary---bluetooth detectable updates
The update process for Bluetooth name--title is completed in Bluetoothsettings.java, such as the following steps
- Get to native Bluetooth name
Mmydevicepreference.settitle (Mlocaladapter.getname ());
- A popup dialog box is processed when you rename a Bluetooth operation
New Bluetoothnamedialogfragment (). Show ( Getfragmentmanager (), "Rename Device");
In Bluetoothnamedialogfragment.java, listen for the edit box in the dialog box. If edited, change the name of the local Bluetooth, which is dedicated to renaming the native Bluetooth.
Mlocaladapter.setname ();
The program does not run the Onresume method after the Current Activity popup dialog box disappears. So register the broadcast in the Bluetoothsettings.java
- Bluetoothadapter.action_local_name_changed broadcasts are sent when the local Bluetooth name changes. Bluetoothsettings.java to update the title of Mmydevicepreference after hearing the broadcast
Updated display of Bluetooth detectable---Summary
The display update for summary is completed in Bluetoothdiscoverableenabler.java, which is dedicated to updating summary and handling Mmydevicepreference Click events
- Register broadcast Monitor the status of Bluetooth scan changes, when the Bluetooth scan status changes will send Bluetoothadapter.scan_mode_connectable_discoverable broadcast. Updates the summary display. method to invoke the third step
- Set click-Listen for preference, change scan status
- Display summary based on the scan status of local Bluetooth
There are two cases when displaying summary,
I>. Assume that local Bluetooth is both capable of scanning and can be detected when it is in the scan_mode_connectable_discoverable state. is displayed according to the length of the detectable time, the content is: all nearby equipment can be detected to +timeout
Ii> If it is a different state, it will be displayed as "paired device visible" or "not visible to all devices" depending on whether a paired device is already present
Now that's a detectable test. Directly say that can be measured time, in the program started the registration of the broadcast Bluetoothdiscoverabletimeoutreceiver, When the detectable time is over, the scan status of Bluetooth is set to bluetoothadapter.scan_mode_connectable, that is, the detection of all devices can be canceled.
Localbluetoothadapter.setscanmode (bluetoothadapter.scan_mode_connectable);
When the detectable resistance is set to a fixed period of time, an alarm is set to trigger the broadcast, which is triggered when the specified time arrives. Turn off the detectable nature of Bluetooth on your phone, assuming you want to be able to detect it permanently. You just need to say that the alarm clock is canceled, no longer triggering the broadcast
Intent Intent = new Intent (intent_discoverable_timeout); Intent.setclass (context, bluetoothdiscoverabletimeoutreceiver.class); Pendingintent pending = Pendingintent.getbroadcast ( context, 0, intent, 0); Alarmmanager Alarmmanager = (Alarmmanager) Context.getsystemservice (context.alarm_service); if (pending! = NULL) { //Cancel Any previous alarms this same thing. Alarmmanager.cancel (pending); LOG.D (TAG, "Setdiscoverablealarm (): Cancel prev Alarm"); } Pending = Pendingintent.getbroadcast ( context, 0, intent, 0); Alarmmanager.set (Alarmmanager.rtc_wakeup, AlarmTime, pending);
For the detectable time when the arrival of the Bluetooth can detect the detection of the settings in the Bluetoothdiscoverabletimeoutreceiver.java. This class is a broadcast component. Specially designed to turn on or off the detectable alarm timing and turn off the detectable resistance.
three, device list related
Join paired sets List
- Create Preferencecategory type to pair device List Object Mpaireddevicescategory
Mpaireddevicescategory = new Preferencecategory (getactivity ());
- Join the list of paired devices Mpaireddevicescategory
Adddevicecategory (Mpaireddevicescategory, r.string.bluetooth_preference_paired_devices, Bluetoothdevicefilter.bonded_device_filter);
- Calls such as the following methods to pass a list of paired devices to devicelistpreferencefragment for management
Setdevicelistgroup (Preferencegroup);
The above code is finished in Bluetoothsettings, then the list is managed in Devicelistpreferencefragment
- Get to the device cache list, which holds paired devices and non-paired devices, which are read by Bluetoothadapter's Getbondeddevices method to the cached list after the program is successfully installed
- Adding paired devices to the list is the bluetoothdevicepreference constructed preference, which means that the management of preference for a single device is in Bluetoothdevicepreference
Join the list of available devices nearby
- Click to scan nearby available devices
- Display a list of nearby available devices to the screen
Getpreferencescreen (). Addpreference (Mavailabledevicescategory);
- Cache to cache list after scan to device and display to nearby list of available devices
- If the list of available devices nearby is empty, remove
The device's Click event is processed in Bluetoothdevicepreference. Different devices have different actions: assuming that the paired device is clicked, the connection is made. Suppose you are pairing a paired device after you click it. Assume that a connected device is clicked and disconnected.
android4.42-settings Bluetooth module for source Code analysis overall implementation (total)