Android网络开发之蓝牙

蓝牙采用分散式网络结构以及快调频和短包技术,支持点对点及点对多点通信,工作在全球通用的2.4GHz ISM(I-工业、S-科学、M-医学)频段,其数据速率为1Mbps,采用时分双工传输方案。

 

蓝牙协议可分4层:

1. 核心协议层

2. 电缆替代协议层

3. 电话控制协议层

4. 其他协议层

 

蓝牙协议之核心协议层包括

1. 基带,

2. 链路管理, LMP, 负责蓝牙组件间连接的建立,

3. 逻辑链路控制和适应协议, L2CAP,

4. 业务搜寻协议, SDP, 通过SDP可以查询设备信息、业务及业务特征、并在查询之后建立两个或多个蓝牙设备间的连接。SDP支持3种查询方式:按业务类别搜索,按业务熟悉搜索,业务浏览。

 

蓝牙是android 2.0的包,在建立android工程时,一定要将API Level等级设置为5,即android 2.0。

蓝牙api位于android.bluetooth中,提供的常用功能有

1. BluetoothAdapter

2. BluetoothClass

3. BluetoothClass.Device

4. BluetoothClass.Device.Major

5. BluetoothClass.Service

6. BluetoothDevice

7. BluetoothServerSocket

8. BluetoothSocket

这些api允许:

app连接和断开蓝牙耳机、蓝牙打印机、蓝牙扫描仪等设备;

编写和修改本地服务的SDP协议数据库和查询其他蓝牙设备上的SDP协议数据库;

在android上建立RFCOMM协议的连接并连接到其他指定设备。

 

示例:学习蓝牙api的使用

 

// 在AndroidManifest.xml文件中声明等级和授权

<uses-sdk android:minSdkVersion=”5”/>

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

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

 

// 取得蓝牙适配器

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

// 打开蓝牙

bluetooth.enable();

// 关闭蓝牙

bluetooth.disable();

// 请求打开蓝牙常量

private static final int REQUEST_ENABLE = 0X1;

// 请求允许被搜索常量

private static final int REQUEST_DISCOVERABLE = 0X2;

// 请求打开蓝牙

Intent it = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(it, REQUEST_ENABLE);

// 请求能够被搜索

Intent it = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

startActivityForResult(it, REQUEST_DISCOVERABLE);

 

BluetoothAdaper中的ACTION常量

1. ACTION_DISCOVERY_FINISHED

2. ACTION_DISCOVERY_STARTED

3. ACTION_LOCAL_NAME_CHANGED

4. ACTION_REQUEST_DISCOVERABLE

5. ACTION_REQUEST_ENABLE

6. ACTION_SCAN_MODE_CHANGED

7. ACTION_STATE_CHANGED

BluetoothAdapter中的常用方法

1. cancelDiscovery

2. checkBluetoothAddress

3. disable

4. enable

5. getAddress

6. getDefalutAdpater

7. getName

8. getRemoteDevice

9. getScanMode

10. getState

11. isDiscovering

12. isEnabled

13. setName

14. startDiscovery

 

// 学习如何搜索网络上的设备并显示

// 搜索到一个指定地址的蓝牙设备BluetoothDevice

private BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

private List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();

private volatile boolean discoveryFinished;

// 搜索到一个蓝牙设备

BroadcastReceiver findReceiver = new BroadcastReceiver(

  public void onReceive(Context context, Intent intent) {

    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

    devices.add(device);

    showDevices();

  }

// 搜索蓝牙设备工作结束

BroadcastReceiver finishDiscoveryReceiver = new BroadcastReceiver(

  public void onReceive(Context context, Intent intent) {

    unregisterReceiver(findReceiver);

    unregisterReceiver(this);

    discoveryFinished = true;

  }

);

在onCreate()方法中

// 注册Receiver

registerReceiver(finishDiscoveryReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));

registerReceiver(findReceiver, new IntentFilter(BluetoothAdapter.ACTION_FOUND));

// 显示对话框 正在搜索蓝牙设备

SampleUtils.indeterminate(mContext, mHandler, “扫描中…”, mDiscoveryWorker, new OnDismissListener(){

  public void onDismiss(DialogInterface dialog) {

    if(bluetooth.isDiscovering()){

      bluetooth.cancelDiscovery();

    }

    discoveryFinished = true;

  }

});

// mDiscoveryWorker

Runnable mDiscoveryWorker = new Runnable(){

  public void run(){

    bluetooth.startDiscovery();

    while(!discoveryFinished){

      try{

        Thread.sleep(100);

      }catch(InterruptedException ex){ }

    }

  }

};

在showDevices()方法中

List<String> data = new ArrayList<String>();

for(int i=0;i<devices.size;i++){

  BluetoothDevice device = devices.get(i);

  String desc = “name:(”+device.getName()+”), address:(”+device.getAddress()+”)";

  data.add(desc);

}

本例的Activity继承ListActivity

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

mHanlder.post(new Runnable(){

  public void run(){

    setListAdapter(adapter);

  }

});

重写ListActivity的onListItemClick方法

protected void onListItemClick(ListView listView, View, view, int position, long id){

  Intent it = new Intent();

  it.putExtra(BluetoothDevice.EXTRA_DEVICE, devices.get(position));

  setResult(RESULT_OK, it);

}

原文地址:https://www.cnblogs.com/fengzhblog/p/3186531.html