Android蓝牙栈bluez使用方法
本文来自:安卓航班网
很多开发人员在最新的Android 0.9 SDK中没有找到蓝牙栈相关的参考,不过在官方消息中我们已经看到了是采用的是org.bluez相关的Package。这里主要介绍下Android蓝牙SDP相关的例程,我们看到是.intent.action,分为4个阶段:
org.bluez.intent.action.DISCOVERY_STARTED 开始探测
org.bluez.intent.action.REMOTE_DEVICE_FOUND 远程设备找到
org.bluez.intent.action.REMOTE_NAME_UPDATED获取远程设备名称
org.bluez.intent.action.DISCOVERY_COMPLETED完成SDP
大概的使用方法如下,在开发板上可以测试下,首先移植下org.bluez库
public void onStart(int startId, Bundle args)
{
super.onStart( startId, args );
try{
IBluetoothService ibtservice = BluetoothServiceNative.getBluetoothService();//获取一个实例,其实在Android中已经准备好了系统服务
if( ibtservice == null ) {
stopSelf();//失败了就Exit
return;
}
manager=ibtservice.getManager();
if( manager == null ) {
stopSelf();
}
if(! ibtservice.isBluetoothStarted()){
ibtservice.startBluetooth();//开始服务
}
miadapter=manager.getDefaultAdapter();
miadapter.startDiscovery();//准备探测了
String[] dispositivos=miadapter.listRemoteDevices();//远程设备mac列表
if (dispositivos.length==0){
Toast.makeText(this, R.string.no_found_devices, Toast.LENGTH_SHORT).show();//没有找到蓝牙设备
}else{
for (int i=0; i<dispositivos.length;i++){
//已经找到的蓝牙设备,准备配对然后干什么就看你自己了,当然比如获取设备类型是handset、ftp还是别的,根据Mac可以初步判断,当然标准GPS会使用串口来通讯。
}
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
打开通讯后可以导入下面的类
import org.bluez.Manager;
import org.bluez.Adapter;
import org.bluez.IBluetoothService;
import org.bluez.BluetoothServiceNative;
具体的Android蓝牙栈相关的以后再讲吧。
更多相关文章
- [转]Android中的BatteryService及相关组件
- S5PV210-uboot-fastboot原理
- android usb解析(二)UsbHostManager(and6.0)
- input subsystem
- Android清单文件中相关属性含义(Provider)
- 基于Cortex-A8的fs_s5pc100开发平台研发成功
- android 中文 api (72) ―― BluetoothSocket[蓝牙]
- [Android实例] android登录Web以及登录保持,cookie管理相关
- adb wifi连接手机