android 蓝牙4.0 开发介绍,android4.0

android 蓝牙4.0 开发介绍,android4.0


     最近一直在研究一个蓝牙功能 由于本人是菜鸟  学起来比较忙 一直搞了好久才弄懂 , 网上对蓝牙4.0也就是几个个dome 抄来抄去,全是英文注解 , 对英语不好的朋友来说

真是硬伤 , 一些没必要的描述罗里吧嗦 , 关键的方法接口 一笔带过 .........算了不吐槽了.我就介绍一下我最近的学习心得吧 ,简单的什么开启  蓝牙 搜索蓝牙什么的我就不说了 你们百度一下 android 蓝牙 4.0 一大坨.看完再来看我的博客也行  ,我就介绍点 网上那些 一笔带过 比较重要的接口回调 之类的.

   蓝牙 中具体有些什么东西:  蓝牙名字 (name) 蓝牙地址(adress)  蓝牙信号强度 (riss) 蓝牙服务(service) 蓝牙服务下面又有小服务  也就是具体要做的   这些应该就是我们常用的 其他的我也没用过 也不知道.嘿嘿

  蓝牙通讯过程 :  打开 查找(可以指定uuid查找设备) 连接  就不说了;连接之后  首先 获取 bluetooth service  (通过UUID 查找的)  然后再找bluetooth service 里面的小服务 叫 bluetooth

Characteristic  (也是通过UUID获取的)  之后你就可以 对其进行读写了 ,读写有三种情况 说了好像也没什么用 根据自己的需求吧 ; 


下面就介绍一下
BluetoothGattCallback   网上都是一笔带过根本没有什么解释的  找了好久表示好心累然后自己用设备一个一个方法调:下面贴代码 加注释里面是有发送数据的:
BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
//连接状态触发
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.d("Bluetooth", "-------ConnectSucceed------");
            bluetoothGatt.discoverServices();//发现服务
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.d(" Bluetooth", "-------Disconnect------");
            if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
                bluetoothAdapter = bluetoothManager.getAdapter();
                bluetoothAdapter.enable();
                bluetoothAdapter.startLeScan(uuid, leScanCallback);
            }
        }
    }

    /**
     *  发现设备服务回调
     */
    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            final byte[] buffer = new byte[5];
            String open = "open=1";//发送的数据
            try {
                byte[] data = open.getBytes("UTF-8");
                writeDataToDevice(data);//发送(单独一个方法)
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.d("000", "--------发送数据成功 ----- status:");
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        Log.d("蓝牙回复数据为", characteristic.getValue().toString());
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
      
    }
.....
};

/**
 * 向设备发送数据
 */
public void writeDataToDevice(byte[] data) {
    if (bluetoothGatt == null) {
        Log.d("00 ", "----------------BluetoothGatt=null-----------------------");
        return;
    }

    bluetoothGattService = bluetoothGatt
            .getService(BLURTOOTH_SERVICE);//蓝牙设备中需要使用的服务的UUID
    if (bluetoothGattService == null) {
        Log.d("pcg", "service not found!");
        bluetoothGatt.disconnect();
        return;
    }

    BluetoothGattCharacteristic mCharac = bluetoothGattService
            .getCharacteristic(BLUETOOTH_CHARACTERISTIC);//需要使用该服务下具体某个特性的UUID
    if (mCharac == null) {
     //没有找到的情况
        bluetoothGatt.disconnect();
        return;
    }
    bluetoothGatt.setCharacteristicNotification(mCharac, true);
    Log.d("pcg", "data:" + Arrays.toString(data));
    mCharac.setValue(data); //设置需要发送的数据
    try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    writeCharacteristic(mCharac);
}

就介绍 到这里 吧  看完这个下载个dome  看一下 会清晰一点 .  搞开发还是 看看好  不要去拷贝人家的  ,  看懂了就用的灵活点 ,做这个功能发现全天下的dome 都是一个人写的似的 全是英文  开始怀疑大家都只是拷贝官方的dome  连交互都是一大坨的广播发来发去  真是看都不愿看.就介绍到这里  ,  写的不好 勿喷!

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值