If you have any other questions, please check here.<a href="http://blog.csdn.net/djy1992/article/details/10144843"> <font color="red">[置顶]</font>Android 蓝牙开发(整理大全) </a>
* <a href='https://github.com/sheep0704/DuBluetooth/blob/master/Bluetooth/src/com/immqy/MainActivity.java'> MainActivity</a><br/>
- V1.0.0
-- 根据别人修改的一个版本 -- 给大家作为参考学习使用
- V0.0.9
-- 代码测试使用
Please see
* MainActivity
* SettingActivity
* SearchActivity
* ComminuteActivity
MainActivity -> onActivityResult .
//返回消息处理
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
try {
bluetooth.comminute(openLockBytes, 1);
Thread.sleep(100);
String path = BluetoothUtils.getPath(uri, MainActivity.this);
bluetooth.sendKey((BluetoothUtils.createKey(path)).getBytes());
Thread.sleep(100);
lockBt.setImageDrawable(getResources().getDrawable(R.drawable.open_lock));
} catch (Exception e) {
Log.e("openKey", e.toString());
}
}
super.onActivityResult(requestCode, resultCode, data);
}
MainActivity -> BluetoothDevice.
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice receiverDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (isLock(receiverDevice)) {
device = receiverDevice;
}
}
bluetooth = new Bluetooth(device, BluetoothUtils.readState());
bluetooth.connect();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (BluetoothUtils.isConnect) {
try {
if (isAutomatic) {
startService(new Intent(MainActivity.this, OpenService.class));
break;
}
} catch (Exception e) {
break;
}
}
}
}
});
thread.start();
}
MainActivity -> receiveDevice.
//查看
private void receiveDevice() {
bluetoothAdapter.startDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
receiver = new BluetoothReceiver();
registerReceiver(receiver, filter);
}
Copyright (c) 2016-present, DuBluetooth Contributors. Author Immqy.com .
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.