Android Bluetooth Stack bluez usage

Source: Internet
Author: User

In the modern smart phone field, the Bluetooth function has become very common, so how to develop Bluetooth in the Android Application Development Process, today we will introduce the use of Android Bluetooth Stack bluez.

Many developers did not find any reference for the Bluetooth stack in the latest Android 0.9 SDK. However, we have seen in official news that the Package is org. bluez. Here we will mainly introduce the Android blue tooth SDP Related Routines. We can see that it is. intent. action, which is divided into four stages:


[Java]
Org. bluez. intent. action. DISCOVERY_STARTED // start the probe
Org. bluez. intent. action. REMOTE_DEVICE_FOUND // remote device found
Org. bluez. intent. action. REMOTE_NAME_UPDATED // obtain the remote device name.
Org. bluez. intent. action. DISCOVERY_COMPLETED // complete SDP

Org. bluez. intent. action. DISCOVERY_STARTED // start the probe
Org. bluez. intent. action. REMOTE_DEVICE_FOUND // remote device found
Org. bluez. intent. action. REMOTE_NAME_UPDATED // obtain the remote device name.
Org. bluez. intent. action. DISCOVERY_COMPLETED // complete SDP

The general usage method is as follows. You can test it on the Development Board. First, port the org. bluez library.


[Java]
Public void onStart (int startId, Bundle args)
{
Super. onStart (startId, args );
Try {
Iblustmthservice ibtservice = descrithservicenative. getjavasthservice (); // obtain an instance. In fact, the system service has been prepared in Android.
If (ibtservice = null ){
StopSelf (); // if it fails, Exit
Return;
}
 
Manager = ibtservice. getManager ();
If (manager = null ){
StopSelf ();
}
 
If (! Ibtservice. isw.thstarted ()){
Ibtservice. startBluetooth (); // start the service
}
Miadapter = manager. getdefaadapter adapter ();
Miadapter. startDiscovery (); // you have prepared for the probe.
 
String [] dispositivos = miadapter. listRemoteDevices (); // remote device mac list
 
If (dispositivos. length = 0 ){
Toast. makeText (this, R. string. no_found_devices, Toast. LENGTH_SHORT). show (); // No bluetooth device found
} Else {
For (int I = 0; I <dispositivos. length; I ++ ){
// If you have already found a bluetooth device, prepare for pairing and then perform the matching on your own. Of course, you can determine whether the device type is handset, ftp, or another device type based on Mac, of course, standard GPS uses serial ports for communication.
}
}
} Catch (Exception e ){
System. out. println (e. getMessage ());
}
}

Public void onStart (int startId, Bundle args)
{
Super. onStart (startId, args );
Try {
Iblustmthservice ibtservice = descrithservicenative. getjavasthservice (); // obtain an instance. In fact, the system service has been prepared in Android.
If (ibtservice = null ){
StopSelf (); // if it fails, Exit
Return;
}

Manager = ibtservice. getManager ();
If (manager = null ){
StopSelf ();
}

If (! Ibtservice. isw.thstarted ()){
Ibtservice. startBluetooth (); // start the service
}
Miadapter = manager. getdefaadapter adapter ();
Miadapter. startDiscovery (); // you have prepared for the probe.

String [] dispositivos = miadapter. listRemoteDevices (); // remote device mac list

If (dispositivos. length = 0 ){
Toast. makeText (this, R. string. no_found_devices, Toast. LENGTH_SHORT). show (); // No bluetooth device found
} Else {
For (int I = 0; I <dispositivos. length; I ++ ){
// If you have already found a bluetooth device, prepare for pairing and then perform the matching on your own. Of course, you can determine whether the device type is handset, ftp, or another device type based on Mac, of course, standard GPS uses serial ports for communication.
}
}
} Catch (Exception e ){
System. out. println (e. getMessage ());
}
}

After opening the communication, you can import the following classes.


[Java]
Import org. bluez. Manager;
Import org. bluez. Adapter;
Import org. bluez. ibluw.thservice;
Import org. bluez. descrithservicenative;

Import org. bluez. Manager;
Import org. bluez. Adapter;
Import org. bluez. ibluw.thservice;
Import org. bluez. descrithservicenative;

In addition, we suspect that the final Android platform supports the Bluetooth feature. As a smart platform that does not support Bluetooth, it is so ridiculous. The following is a message from the official side, android 1.0 supports Bluetooth. The final version will include Bluetooth API support. For example, Android supports Bluetooth headsets. Although Bluetooth-related information is not found in earlier versions of the SDK, many features will be included.

We can pass the iblustmthdevicecallback object in multiple asynchronous callbacks, but as an internal interface, we can only see the emergence of the aidl language through iblustmthdevicecallback. AIDL. Android Bluetooth Stack bluez 4.x supports GAP and SDP. The following features are supported:

Bind the GAP and SDP functions, support RFCOMM and SCO sockets, may support L2CAP socket, this function is under consideration, you can use headphones or hands-free mode.

 

Related Article

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.