Extracted from the Internet, from http://www.eoeandroid.com/thread-246-1-1.html
Commonly used intent for Bluetooth settings. The following is a function for registering a Bluetooth intent in thinkthsettings. Java:
Private Boolean initgatethapi (){
Mintentfilter =
// Intent returned when connected to the remote Bluetooth device
New intentfilter (effecthintent. remote_device_connected_action );
// Intent mintentfilter. addaction (effecthintent. remote_device_disconnected_action) returned when the remote Bluetooth device is disconnected );
// The intent received when pairing with the remote Bluetooth device, provided that the other device initiates the pairing.
// To receive the intent
Mintentfilter. addaction (effecthintent. bonding_created_action );
// Intent received when the local Bluetooth device is available
Mintentfilter. addaction (incluthintent. enabled_action );
// Intent received when the local Bluetooth device is unavailable
Mintentfilter. addaction (incluthintent. disabled_action );
// The intent mintentfilter. addaction (incluthintent. remote_device_found_action) received when the remote device is scanned );
// Intent mintentfilter. addaction (incluthintent. remote_device_disappeared_action) received when the remote Bluetooth device disappears );
// The intent received when the remote Bluetooth device name is changed, because the device name has not been obtained yet. // its name
Mintentfilter. addaction (effecthintent. remote_name_updated_action );
// Intent received when a remote device initiates a pairing request
Mintentfilter. addaction (incluthintent. pairing_request_action );
// Intent when the Bluetooth headset status changes
Mintentfilter. addaction (effecthintent. headset_state_changed_action );
// Scanning device ends
Mintentfilter. addaction (incluthintent. discovery_completed_action );
// Start scanning
Mintentfilter. addaction (incluthintent. discovery_started_action );
// The Bluetooth device mode is changed, indicating whether the local Bluetooth device can be searched
Mintentfilter. addaction (effecthintent. mode_changed_action );
// With earphones inserted
Mintentfilter. addaction (intent. action_headset_plug );
Start Bluetooth
When starting the Bluetooth, note that the Bluetooth cannot be started normally, because during normal start, the bluetoothintent will be returned. enabled_action: intent. When an exception occurs during startup, no intent is returned. Android uses the callback function to solve this problem. The following is the Enable (iblustmthdevicecallback callback) process in javasthdeviceservice. Java:
Public synchronized Boolean enable (iblustmthdevicecallback callback ){
Checkpermissionpolicthadmin ();
Log. D (TAG, "Start enable! ");
// Airplane mode can prevent Bluetooth radio from being turned on.
If (misairplanesensitive & isairplanemodeon ()){
Return false;
}
If (misenabled ){
Return false;
}
If (menablethread! = NULL & menablethread. isalive ()){
Return false;
}
// The main startup process is put in a new thread, but whether it can be started or not
// Still returns true
Menablethread = new enablethread (callback );
Menablethread. Start ();
//
Return true;
}
Private enablethread menablethread;
Private class enablethread extends thread {
Private Final iblustmthdevicecallback menablecallback;
Public enablethread (iblustmthdevicecallback callback ){
Menablecallback = callback;
}
Public void run (){
Boolean res = enablenative ();
If (RES ){
Meventloop. Start ();
}
If (menablecallback! = NULL ){
Try {
// Use the callback function to indicate whether the bluetooth device is enabled normally.
Menablecallback. onenableresult (res?
Effecthdevice. result_success:
Descrithdevice. result_failure );
} Catch (RemoteException e ){}
}
If (RES ){
Misenabled = true;
Misdiscovering = false;
Intent intent = new intent (incluthintent. enabled_action );
Mcontext. sendbroadcast (intent );
}
} Else {
Misenabled = false;
Misdiscovering = false;
}
Menablethread = NULL;
}
}
// This callback function will be passed as a parameter to the Enable (iblustmthdevicecallback callback) in bluetoothservice)
Static class devicecallback extends iblustmthdevicecallback. Stub {
Handler messagehandler;
Public void sethandler (handler ){
Synchronized (this ){
Messagehandler = handler;
}
Public void onenableresult (INT result ){
Switch (result ){
// Executed when startup fails
Case when thdevice. result_failure:
Messagehandler. sendmessage (messagehandler. obtainmessage (event_failed_bt_enable, 0 ));
Break;
}
}
// Executed when pairing is completed
Public void oncreatebondingresult (string address, int result ){
Synchronized (this ){
If (messagehandler! = NULL ){
If (result = descrithdevice. result_failure ){
Messagehandler. sendmessage (messagehandler. obtainmessage (
Handle_pairing_failed, address ));
} Else {
Messagehandler. sendmessage (messagehandler. obtainmessage (
Handle_pairing_passed, address ));
}
}
}
}
};
Closing Process
Public synchronized Boolean disable (){
Checkpermissionpolicthadmin ();
If (menablethread! = NULL & menablethread. isalive ()){
Return false;
}
If (! Misenabled ){
Return true;
}
If (! Disablenative ()){
Log. D (TAG, "disablenative false ");
Return false;
}
Meventloop. Stop ();
Misenabled = false;
Misdiscovering = false;
Intent intent = new intent (incluthintent. disabled_action );
Mcontext. sendbroadcast (intent );
Return true;
}
Pairing Process
Private void dopair (preference pref, string address ){
Pref. setenabled (false );
Pref. setsummary (str_pairing );
If (mpinedit! = NULL ){
String strpin = mpinedit. gettext (). tostring ();
Mbluetooth. writepincode (address, strpin );
Mbluetooth. createbonding (address, sdevicecallback );
}
}
Finally, the oncreatebondingresul of the callback function is executed based on the matching result.