private void search() {
BluetoothAdapter adapter = BluetoothAdaptergetDefaultAdapter();
if (!adapterisEnabled()) {
adapterenable();
}
Intent enable = new Intent(BluetoothAdapterACTION_REQUEST_DISCOVERABLE);
enableputExtra(BluetoothAdapterEXTRA_DISCOVERABLE_DURATION, 3600); //3600为蓝牙设备可见时间
startActivity(enable);
Intent searchIntent = new Intent(this, ComminuteActivityclass);
startActivity(searchIntent);
}
首先,需要获得一个BluetoothAdapter,可以通过getDefaultAdapter()获得系统默认的蓝牙适配器,当然我们也可以自己指定,但这个真心没有必要,至少我是不需要的。然后我们检查手机的蓝牙是否打开,如果没有,通过enable()方法打开。接着我们再设置手机蓝牙设备的可见,可见时间可以自定义。
完成这些必要的设置后,我们就可以正式开始与蓝牙模块进行通信了:
public class ComminuteActivity extends Activity {
private BluetoothReceiver receiver;
private BluetoothAdapter bluetoothAdapter;
private List<String> devices;
private List<BluetoothDevice> deviceList;
private Bluetooth client;
private final String lockName = "BOLUTEK";
private String message = "000001";
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
superonCreate(savedInstanceState);
setContentView(Rlayoutsearch_layout);
listView = (ListView) thisfindViewById(Ridlist);
deviceList = new ArrayList<BluetoothDevice>();
devices = new ArrayList<String>();
bluetoothAdapter = BluetoothAdaptergetDefaultAdapter();
bluetoothAdapterstartDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDeviceACTION_FOUND);
receiver = new BluetoothReceiver();
registerReceiver(receiver, filter);
listViewsetOnItemClickListener(new AdapterViewOnItemClickListener() {
@Override
public void onItemClick(AdapterView<> parent, View view, int position, long id) {
setContentView(Rlayoutconnect_layout);
BluetoothDevice device = deviceListget(position);
client = new Bluetooth(device, handler);
try {
clientconnect(message);
} catch (Exception e) {
Loge("TAG", etoString());
}
}
});
}
@Override
protected void onDestroy() {
unregisterReceiver(receiver);
superonDestroy();
}
private final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msgwhat) {
case BluetoothCONNECT_FAILED:
ToastmakeText(ComminuteActivitythis, "连接失败", ToastLENGTH_LONG)show();
try {
clientconnect(message);
} catch (Exception e) {
Loge("TAG", etoString());
}
break;
case BluetoothCONNECT_SUCCESS:
ToastmakeText(ComminuteActivitythis, "连接成功", ToastLENGTH_LONG)show();
break;
case BluetoothREAD_FAILED:
ToastmakeText(ComminuteActivitythis, "读取失败", ToastLENGTH_LONG)show();
break;
case BluetoothWRITE_FAILED:
ToastmakeText(ComminuteActivitythis, "写入失败", ToastLENGTH_LONG)show();
break;
case BluetoothDATA:
ToastmakeText(ComminuteActivitythis, msgarg1 + "", ToastLENGTH_LONG)show();
break;
}
}
};
private class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intentgetAction();
if (BluetoothDeviceACTION_FOUNDequals(action)) {
BluetoothDevice device = intentgetParcelableExtra(BluetoothDeviceEXTRA_DEVICE);
if (isLock(device)) {
devicesadd(devicegetName());
}
deviceListadd(device);
}
showDevices();
}
}
private boolean isLock(BluetoothDevice device) {
boolean isLockName = (devicegetName())equals(lockName);
boolean isSingleDevice = devicesindexOf(devicegetName()) == -1;
return isLockName && isSingleDevice;
}
private void showDevices() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, androidRlayoutsimple_list_item_1,
devices);
listViewsetAdapter(adapter);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)