简述: 

关于蓝牙的开发,是从sdk2.0版本开始支持的,模拟器不支持,必须使用真机进行测试

android中所有半球蓝牙开发的类都在android.bluetooth包下


权限:

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />


开发蓝牙功能相关类的分析



BluetoothAdapter类

代表了一个本地的蓝牙适配器。它是所有蓝牙交互的入口点。利用它你可以发现其他蓝牙设备,查询绑定了的设备,使用已知的MAC地址实例化一个蓝牙设备和建立一个BluetoothServerSocket(作为服务器端)来监听来自其他设备的连接。


BluetoothDevice类

代表了一个远端的蓝牙设备,使用它请求远端蓝牙设备连接或者获取远端蓝牙设备的名称、地址、种类和绑定状态(其信息是封装在BluetoothSocket中)。


BluetoothSocket类

代表了一个蓝牙套接字的接口(类似于TCP中的套接字),它是应用程序通过输入、输出流与其他蓝牙设备通信的连接点。


BlueboothServerSocket类

代表打开服务连接来监听可能到来的连接请求(属于server端),为了连接两个蓝牙设备必须有一个设备作为服务器打开一个服务套接字。当远端设备发起连接连接请求的时候,并且已经连接到了的时候,BlueboothServerSocket类将会返回一个BluetoothSocket。


BluetoothAdapter  蓝牙适配器,

在建立bluetoothSocket之前,需要不断操作它

常用的相关方法:



cancelDiscovery()  取消发现,当我们正在搜索设备的时候,调用这个方法,将取消搜索

disable()  将关闭蓝牙

enable() 打开蓝牙,使用这个方法打开蓝牙不会弹出提示


Intent  intent = new Intent(BluetoothAdapter.ACTIO N_REQUEST_ENABLE)

startActivityForResrlt(intent,code);

使用这个方法打开蓝牙将会提示用户

getAddress()获取本地蓝牙地址
getDefaultAdapter()获取默认BluetoothAdapter
getName()获取本地蓝牙名称
getRemoteDevice(String address)根据蓝牙地址获取远程蓝牙设备
getState()获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要)
isDiscovering()判断当前是否正在查找设备,是返回true
isEnabled()判断蓝牙是否打开,已打开返回true,否则,返回false

listenUsingRfcommWithServiceRecord(String name,UUID uuid)根据名称,UUID创建并返回BluetoothServerSocket

startDiscovery()开始搜索


BluetoothDevice用来描述了一个蓝牙设备

       createRfcommSocketToServiceRecord(UUIDuuid)   根据UUID创建并返回一个BluetoothSocket也就是创建BluetoothSocket


BluetoothServerSocket

accept(),

accept(inttimeout)

这两个方法是用来接收客户端请求,应该放在新的线程中进行操作   


BluetoothSocket   客户端

       close(),关闭

       connect()连接

       getInptuStream()获取输入流

       getOutputStream()获取输出流

       getRemoteDevice()获取远程设备,这里指的是获取bluetoothSocket指定连接的那个远程蓝牙设备



实例:配制本机蓝牙模块


启动蓝牙功能


        //首先通过调用静态方法getDefaultAdapter()获取蓝牙适配器BluetoothAdapter
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            // Device does not support Bluetooth
            return;
        }

        //调用isEnabled()来查询当前蓝牙设备的状态,如果返回为false,则表示蓝牙设备没有开启,
        //封装一个ACTION_REQUEST_ENABLE请求到intent里面,调用startActivityForResult()方法使能蓝牙设备
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 20);
        }

查询已配对好的蓝牙设备



 //在建立连接之前你必须先查询配对好了的蓝牙设备集以便选取一个设备进行通信
        Set<BluetoothDevice> pairedDevices =mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            //Loop through paired devices
            for (BluetoothDevice device : pairedDevices) {
               // mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        }


扫描设备 


 // 用ACTION_FOUND为action注册广播接收器
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // 发现设备
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // 从Intent中获取蓝牙设备
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // 添加名字和地址到设备适配器中
                //mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        }
    };


注册广播接收


  //注册广播接收器
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter); //在onDestroy时记得注销广播接收器