当前位置:操作系统 > 安卓/Android >>

Android 蓝牙

正文

 

蓝牙是一种支持设备短距离传输数据的无线技术。android在2.0以后提供了这方面的支持。
从查找蓝牙设备到能够相互通信要经过几个基本步骤(本机做为服务器):
1.设置权限
在manifest中配置

 

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

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>2.启动蓝牙
首先要查看本机是否支持蓝牙,获取BluetoothAdapter蓝牙适配器对象


[java]
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
if(mBluetoothAdapter == null){ 
        //表明此手机不支持蓝牙  
        return; 

if(!mBluetoothAdapter.isEnabled()){ //蓝牙未开启,则开启蓝牙  
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 

//......  
public void onActivityResult(int requestCode, int resultCode, Intent data){ 
       if(requestCode == REQUEST_ENABLE_BT){ 
              if(requestCode == RESULT_OK){ 
                   //蓝牙已经开启   
              } 
       } 

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null){
        //表明此手机不支持蓝牙
        return;
}
if(!mBluetoothAdapter.isEnabled()){ //蓝牙未开启,则开启蓝牙
   Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
   startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
//......
public void onActivityResult(int requestCode, int resultCode, Intent data){
       if(requestCode == REQUEST_ENABLE_BT){
              if(requestCode == RESULT_OK){
                   //蓝牙已经开启
              }
       }
}3。发现蓝牙设备
这里可以细分为几个方面
(1)使本机蓝牙处于可见(即处于易被搜索到状态),便于其他设备发现本机蓝牙
[java]
//使本机蓝牙在300秒内可被搜索  
private void ensureDiscoverable() { 
        if (mBluetoothAdapter.getScanMode() != 
            BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { 
            Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); 
            startActivity(discoverableIntent); 
        } 

//使本机蓝牙在300秒内可被搜索
private void ensureDiscoverable() {
        if (mBluetoothAdapter.getScanMode() !=
            BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
            Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
            startActivity(discoverableIntent);
        }
}
(2)查找已经配对的蓝牙设备,即以前已经配对过的设备
[java]
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
if (pairedDevices.size() > 0) { 
    findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); 
    for (BluetoothDevice device : pairedDevices) { 
        //device.getName() +" "+ device.getAddress());  
    } 
} else { 
    mPairedDevicesArrayAdapter.add("没有找到已匹对的设备"); 

  Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
  if (pairedDevices.size() > 0) {
   findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
   for (BluetoothDevice device : pairedDevices) {
    //device.getName() +" "+ device.getAddress());
   }
  } else {
   mPairedDevicesArrayAdapter.add("没有找到已匹对的设备");
  }
(3)通过mBluetoothAdapter.startDiscovery();搜索设备,要获得此搜索的结果需要注册
一个BroadcastReceiver来获取。先注册再获取信息,然后处理
[java]
//注册,当一个设备被发现时调用onReceive  
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
        this.registerReceiver(mReceiver, filter); 
 
//当搜索结束后调用onReceive  
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
        this.registerReceiver(mReceiver, filter); 
//.......  
private BroadcastReceiver mReceiver = new BroadcastReceiver() { 
        @Override 
        public void onReceive(Context context, Intent intent) { 
            String action = intent.getAction(); 
            if(BluetoothDevice.ACTION_FOUND.equals(action)){ 
                 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
                  // 已经配对的则跳过  
                 if (device.getBondState() != BluetoothDevice.BOND_BONDED) { 
                      mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());  //保存设备地址与名字  
&n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,