本文来自:安卓航班网

    很多开发人员在最新的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蓝牙栈相关的以后再讲吧。

原文地址:http://www.apkway.com/forum.php?mod=viewthread&tid=2814&extra=page%3D1

posted on 2011-08-16 21:43  泉と緣  阅读(422)  评论(0编辑  收藏  举报