蓝牙技术(BlueTooth)——(一)

来源:互联网 发布:手机文字识别软件 编辑:程序博客网 时间:2024/06/05 18:49


一,概述


    蓝牙是一种短距离的无线通信技术标准。


    蓝牙协议分为4层,即核心协议层,电缆替代协议层,电话控制协议层,和      采纳的其它协议层。


    这4中协议中最重要的是核心协议。蓝牙的核心协议包括基带,链路管理,逻辑链路控制和适应协议四部分。其中链路管理(LMP)负责蓝牙组件间连接的建立。逻辑链路控制与适应协议(L2CAP)位于基带协议层上,属于数据链路层,是一个为高层传输和应用层协议屏蔽基带协议的适配协议。



二,代码打开蓝牙的方式


  方式一:  


  if(bluetoothAdapter==null || !bluetoothAdapter.isEnabled()){                    Intent enableBtIntent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);                    startActivityForResult(enableBtIntent,1);                }


   第二种方式:


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

 BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();        bluetoothAdapter.enable();        bluetoothAdapter.disable();


三,通过代码搜索蓝牙设备



1,搜索普通蓝牙设备


//3,扫描附近的设备                if(bluetoothAdapter.isDiscovering()){                    bluetoothAdapter.cancelDiscovery();                }else{                    //每次扫描之前都先判断一下是否存在已久配对的设备                    Set<BluetoothDevice> paireDevices=bluetoothAdapter.getBondedDevices();                    if(paireDevices.size()>0){                        for(BluetoothDevice device :paireDevices){                            Map map=new HashMap();                            map.put("deviceName",device.getName());                            map.put("deviceAddress",device.getAddress());                            map.put("bluetooth_status","已配对");                            deviceList.add(map);                        }                        Log.i("之前已经绑定过的设备:",deviceList.toString());                    }                    bluetoothAdapter.startDiscovery();//开始搜索

/*监听扫描过程中的变化*/    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {            String action = intent.getAction();            // When discovery finds a device            if (BluetoothDevice.ACTION_FOUND.equals(action))            {                // Get the BluetoothDevice object from the Intent                // 通过EXTRA_DEVICE附加域来得到一个BluetoothDevice设备                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);                // If it's already paired, skip it, because it's been listed already                // 如果这个设备是不曾配对过的,添加到list列表                if (device.getBondState() != BluetoothDevice.BOND_BONDED)                {                    Message msg=new Message();                    msg.what=1;                    Bundle bundle=new Bundle();                    bundle.putString("deviceName",device.getName());                    bundle.putString("deviceAddress",device.getAddress());                    msg.setData(bundle);                    handler.sendMessage(msg);                    Map map=new HashMap();                    map.put("deviceName",device.getName());                    map.put("deviceAddress",device.getAddress());                    Log.i("扫描到的新设备:",map.toString());                    Log.i("加入新设备之后,扫描到的总设备:",deviceList.toString());                }                // When discovery is finished, change the Activity title            }            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))            {                Log.i("扫描结束------扫描到的总设备:",deviceList.toString());               handler.obtainMessage(0).sendToTarget();            }        }    };



2,搜索BLE设备



 // Device scan callback.    private BluetoothAdapter.LeScanCallback mLeScanCallback =            new BluetoothAdapter.LeScanCallback() {                @Override                public void onLeScan(final BluetoothDevice device, int rssi,                                     byte[] scanRecord) {                    runOnUiThread(new Runnable() {                        @Override                        public void run() {                            Map map=new HashMap();                            map.put("deviceName",device.getName());                            map.put("deviceAddress",device.getAddress());                            map.put("bluetooth_status","BLE设备");                            Log.i("扫描到新的BLE设备:",map.toString());                            deviceList.add(map);                        }                    });                }            };    // Stops scanning after 10 seconds.    private static final long SCAN_PERIOD = 10000;    private void scanLeDevice(final boolean enable) {        if (enable) {            // Stops scanning after a pre-defined scan period.            mHandler.postDelayed(new Runnable() {                @Override                public void run() {                    mScanning = false;                    bluetoothAdapter.stopLeScan(mLeScanCallback);                }            }, SCAN_PERIOD);            mScanning = true;            bluetoothAdapter.startLeScan(mLeScanCallback);        } else {            mScanning = false;            bluetoothAdapter.stopLeScan(mLeScanCallback);        }    }


四,如何用蓝牙进行数据传输


     通过蓝牙传输数据与Socket类似。在网络中使用Socket和ServerSocket控制客户端和服务端的数据读写。而蓝牙通讯也由客户端和服务端Socket来完成。蓝牙客户端Socket是BluetoothSocket,蓝牙服务端Socket是BluetoothServerSocket。这两个类都在android.bluetooth包中。


      无论是BluetoothSocket,还是BluetoothServerSocket,都需要一个UUID来标识,格式如下:

      此UUID是一个8-4-4-4-12的字符串。

      UUID相当于Socket的端口,而蓝牙地址相当于Socket的IP。


      两个蓝牙设备进行连接时,需要使用同一个UUID。一些时候,很多型号的手机(可能是安卓机和水果机)之间使用了不同的程序也可以使用蓝牙进行通讯。表面上看,他们之间几乎不肯能使用同一个UUID。


      实际上,UUID和TCP的端口一样,也有一些默认的值。例如,讲蓝牙模拟成串口的服务就使用了一个标准的UUID:00001101-0000-1000-8000-00805F9B34FB.除此之外,还有很多标准的UUID,比如:

    信息同步服务:00001104-0000-1000-8000-00805F9B34FB.

    文件传输服务:00001106-0000-1000-8000-00805F9B34FB。




PS:附加上写的BLE设备扫描apk:http://download.csdn.net/detail/lhc2207221755/9629293



 





 

0 2