真是应该值得庆祝一下,为了个毕设,本人真是呕心沥血啊!就在刚才,终于完成了蓝牙成功配对的工作,在此将全过程记录下来,以便日后查阅。
蓝牙通讯的全过程大多资料上都有介绍,无非就是先打开蓝牙、搜索设备、被设备搜索、进行配对,这里我就不在多说啦,将部分代码附上
现在Manifest.xml中添加两个权限:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
开启本地蓝牙设备:
蓝牙关闭:
上面两步相对来说简单,至于蓝牙搜索问题,我新建了一个activity窗口,用于显示搜索到的蓝牙设备,并在新窗口中实现选中配对过程,首先,说一下设备搜索,
由主窗口调用adapter.startDiscovery();//adapter为本地蓝牙适配器;再调用startActivityForResult (new Intent(MainActivity.this, Device.class), 1);
开启新创建的Device.class。startDiscovery();是异步完成的,每次搜索到一个设备,便会向系统发送一个广播通知,所以这里要新创建一个广播接收对象,用来
接收所发现的设备信息。代码如下
--------------------------------------------------------------------------------------
其中广播接收的信息被保存到str中,再转存到lst_Devices中。
对于搜索过程中,屏幕实时显示,用了一个list_view来实现的,具体如下:
ListView list_Devices; //用于显示搜索到的设备参数
List<String> lst_Devices = new ArrayList<String>();
final ArrayAdapter<String> adt_Devices;
list_Devices = (ListView) this.findViewById(R.id.listview);
adt_Devices = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, lst_Devices);
list_Devices.setAdapter(adt_Devices);
将list_view的数据源锁定到lst_Devices,至于adt_Devices我也不太清楚了,例子上面都是这么写的。
至于设备配对问题,看似简单,可总是抛出service discovery failed异常
后来在网上找到了解决办法,但是为什么这么做,我也不知道,代码如下:
至此蓝牙配对完成,下一步该是通讯了,有待研究,将代码附上,由于此项目尚未完成,所以部分代码还没有写完,此代码只实现了蓝牙搜索配对
package android.app; import java.util.*; import android.app.Activity; import android.bluetooth.*; import android.content.*; //import android.net.Uri; import android.os.*; import android.view.View; import android.widget.*; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final TextView text = (TextView)this.findViewById(R.id.textview);//用一个textview来提示使用者 //获取本地蓝牙适配器 final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); if(adapter == null) text.setText("本机器没有蓝牙适配器"); else { if(adapter.isEnabled()) text.setText("蓝牙适配器已开启"); else text.setText("请开启蓝牙适配器"); //此按钮用于关闭电灯 Button button_Close = (Button)this.findViewById(R.id.button1); button_Close.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click } }); //此按钮用于打开电灯 Button button_Open = (Button)this.findViewById(R.id.button4); button_Open.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click } }); //此按钮用于调节灯光的亮度 Button button_Up = (Button)this.findViewById(R.id.button2); button_Up.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click } }); //此按钮用于调节灯光的暗度 Button button_Down = (Button)this.findViewById(R.id.button3); button_Down.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click } }); //此按钮仅负责打开蓝牙适配器 Button button_OpenBluetooth = (Button)this.findViewById(R.id.button5); button_OpenBluetooth.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(!adapter.isEnabled()) { //创建一个intent对象,调用系统信息提示用户打开蓝牙 Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); //启动系统调用开始启动蓝牙(打开的过程是异步完成的) startActivity(intent); } else { text.setText("蓝牙适配器已启动"); } } }); //此按钮仅负责关闭蓝牙适配器 Button button_CloseBluetooth = (Button)this.findViewById(R.id.button6); button_CloseBluetooth.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click if(adapter.isEnabled()) { //text.setText(adapter.getAddress()); adapter.disable(); text.setText("蓝牙适配器已关闭"); } } }); //此按钮负责在已建立的配对中,选择要进行通讯的设备,并建立通讯连接,尚未完成 Button button_Socket = (Button)this.findViewById(R.id.button7); button_Socket.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click if(adapter.isEnabled()) { Set<BluetoothDevice> devices = adapter.getBondedDevices(); if(devices.size() > 0) { //利用迭代,将与本机连接的蓝牙设备输出给bluetoothDevice,并且打印其MAX地址 for(Iterator iterator = devices.iterator(); iterator.hasNext();) { BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next(); text.setText(bluetoothDevice.getName()); } } else text.setText("没有已配对的连接,请先配对!"); } else text.setText("请先开启蓝牙适配器!"); } }); //此按钮的最初想法是用于扫描周围设备,并进行配对,已完成搜索并提取地址,配对还未实现 Button button_Search = (Button)this.findViewById(R.id.button8); button_Search.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(adapter.isEnabled()) { if(!adapter.isDiscovering()) { // Perform action on click //开启本地蓝牙搜索功能 adapter.startDiscovery(); //开启Device窗口 //startActivity(new Intent(MainActivity.this, Device.class)); //启动一个新的窗口用于显示搜索到的设备,并且实现设备配对 //上面那个函数用于无参数无返回值的调用新class,下面这个调用时用于有返回值的调用 startActivityForResult (new Intent(MainActivity.this, Device.class), 1); } else adapter.cancelDiscovery(); } else text.setText("请先开启蓝牙适配器!"); } }); } } //重写此函数,android平台会自动调用此函数,用于处理新窗口返回的参数 protected void onActivityResult(int requestCode, int resultCode, Intent data) { String result = data.getExtras().getString("result");//得到新Activity 关闭后返回的数据 } }