Bluetooth蓝牙 (2)

上传人:桔**** 文档编号:558619653 上传时间:2022-07-27 格式:DOC 页数:9 大小:66.50KB
返回 下载 相关 举报
Bluetooth蓝牙 (2)_第1页
第1页 / 共9页
Bluetooth蓝牙 (2)_第2页
第2页 / 共9页
Bluetooth蓝牙 (2)_第3页
第3页 / 共9页
Bluetooth蓝牙 (2)_第4页
第4页 / 共9页
Bluetooth蓝牙 (2)_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《Bluetooth蓝牙 (2)》由会员分享,可在线阅读,更多相关《Bluetooth蓝牙 (2)(9页珍藏版)》请在金锄头文库上搜索。

1、Bluetooth蓝牙 提供的包android.bluetooth。蓝牙功能包:BluetoothAdapter 蓝牙适配器(代表本地蓝牙适配器)BluetoothClass 蓝牙类(主要包括服务和设备)BluetoothClass.Device 蓝牙设备类BluetoothClass.Device.Major 蓝牙设备管理BluetoothClass.Service 有关蓝牙服务的类BluetoothDevice 蓝牙设备(主要指远程蓝牙设备)BluetoothServerSocket 监听蓝牙连接的类BluetoothSocket 蓝牙连接类首先设置权限:其次,要使用蓝牙,首先需要获得蓝牙

2、适配器,BluetoothAdapter _bluetooth=BluetoothAdapter.getDefaultAdapter();/获得本地蓝牙适配器如果要获得远程的蓝牙适配器需要使用BluetoothDevice类。先定义两个常量private static final int REQUEST_ENABLE = 0x1;private static final int REQUEST_DISCOVERABLE = 0x2;1,请求打开蓝牙:Intent enabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startAc

3、tivityForResult(enabler,REQUEST_ENABLE);2,请求能够被搜索Intent enabler=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);startActivityForResult(enabler,REQUEST_DISCOVERABLE);BluetoothAdapter中的动作常量还有:ACTION_DISCOVERY_FINISHED 已完成蓝牙搜索ACTION_DISCOVERY_STARTED 已经开始搜索蓝牙设备ACTION_LOCAL_NAME_CHANGED 更改蓝牙的名字

4、ACTION_REQUEST_DISCOVERABLE 请求能够被搜索ACTION_REQUEST_ENABLE 请求启动蓝牙ACTION_SCAN_MODE_CHANGED 扫描模式已改变ACTION_STATE_CHANGED 状态已改变3,打开蓝牙_bluetooth.enable();4,关闭蓝牙_bluetooth.disable();BluetoothAdapter中的常用方法:cancelDiscovery 取消当前设备搜索的过程checkBluetoothAddress 检查蓝牙地址是否正确 如00:43:A8:23:10:F0 字母字符必须是大写才有效disable 关闭蓝牙

5、适配器enable 打开蓝牙适配器getAddress 取得本地蓝牙设备的适配器地址getDefaultAdapter 得到默认的蓝牙适配器getName 得到蓝牙的名字getRemoteDevice 取得指定蓝牙地址的BluetoothDevice对象getScanMode 取得扫描模式getState 得到状态isDiscovering 是否允许被搜索isEnabled 是否打开setName 设置名字startDiscovery 开始搜索5,搜索蓝牙设备BluetoothDevice实际上是一个蓝牙硬件地址薄,该类对象是不可改变的。其操作都是远程蓝牙硬件使用BluetoothAdapte

6、r来创建一个BluetoothDevice对象。代码清单8-22 public class DiscoveryActivity extends ListActivity private Handler _handler=new Handler(); /取得默认的蓝牙适配器 private BluetoothAdapter _bluetooth=BluetoothAdapter.getDefaultAdapter(); /用来存储搜索到的蓝牙设备 private ListBluetoothDevice) _devices=new ArrayList(); /是否完成搜索 private vola

7、tile boolean _discoveryFinished; private Runnable _discoveryWorker=new Runnable() public void run() _bluetooth.startDiscovery(); for (;) if (_discoveryFinished) break; try Thread.sleep(100); catch (InterruptedException e) ; /接收器,当搜索蓝牙设备完成时调用 private BroadcastReceiver _foundReceiver=new BroadcastRece

8、iver() public void onReceive(Context context,Intent intent) BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); _devices.add(device); showDevices(); ; private BroadcastReceiver _discoveryReceiver=new BroadcastReceiver() public void onReceive(Context context,Intent intent)

9、 unregisterReceiver(_foundReceiver); unregisterReceiver(this); _discoveryFinished=true; ; protected void onCreate(Bundle saveInstance) super.onCreate(.); getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND); setContentView(R.layout.discovery)l

10、 /如果蓝牙适配器没有打开 则结束 if (!_bluetooth.isEnabled() finish(); return; /注册接收器 IntentFilter discoveryFilter=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(_discoveryReceiver,discoveryFilter); IntentFilter foundFilter=new IntentFilter(BluetoothDevice.ACTION_FOUND); registerRec

11、eiver(_foundReceiver,foundFilter); /显示一个对话框,正在搜索蓝牙设备 SamplesUtils.indeterminate(DiscoveryActivity.this,_handler,Scanning.,_discoveryWorkder,new OnDismissListener() public void onDismiss(DialogInterface dialog) for (;_bluetooth.isDiscovering();) _bluetooth.cancelDiscovery(); _discoveryFinished=true;

12、,true); /显示列表 protected void showDevice() List list=new ArrayList(); for (int i=0,size=_devices.size();isize;+i) StringBuilder sb=new StringBuilder(); BluetoothDevice d=_devices.get(i); sb.append(d.getAddress(); sb.append(n); sb.append(d.getName(); String s=sb.toString(); list.add(s); final ArrayAda

13、pter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list); _handler.post(new Runnable() public void run() setListAdapter(adapter); ); protected void onListItemClick(ListView l,View v,int pos,long id) Intent result=new Intent(); result.putExtra(BluetoothDevice.EXTRA_DEVICE,_devices.get(pos); setResult(RESULT_OK,result); finish(); 6,Socket服务端与客户端注册蓝牙服务器: private BluetoothAdapter _bluetooth=BluetoothAdapter.getDefaultAdapter(); private BluetoothServerSocket _serverSocket=blu

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号