Reprint please explain the source!
KQW Siege Lion
Source: Personal Station | CSDN
To get a Git project into your build:
Step 1. Add the Jitpack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects { repositories { ... ‘https://jitpack.io‘ } }}
Step 2. Add the dependency
{ compile ‘com.github.kongqw:AndroidBluetoothManager:1.0.0‘}
Androidbluetoothmanager
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvctq4nzg4mdi=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "alt=" here to write a picture descriptive narrative "title=" ">
Png
Gif
Basic feature Join Permissions
<uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Initialization
new BluetoothManager();
Turn on bluetooth
mBluetoothManager.openBluetooth();
Turn off Bluetooth
mBluetoothManager.closeBluetooth();
Join the Bluetooth switch status monitoring
mBluetoothManager.setOnBluetoothStateListener(this);
/** * Bluetooth callback is being turned off * /@Override Public void Onbluetoothstateturningoff() {//TODO}/** * bluetooth off callback */@Override Public void Onbluetoothstateoff() {//TODO}/** * Bluetooth callback is being turned on * /@Override Public void Onbluetoothstateturningon() {//TODO}/** * Bluetooth turned on callback */@Override Public void Onbluetoothstateon() {//TODO}
Remove the Bluetooth switch status monitoring
mBluetoothManager.removeOnBluetoothStateListener();
Set Bluetooth visible
startActivity(mBluetoothManager.getDurationIntent(0));
Get Bluetooth name
mBluetoothManager.getName()
Change Bluetooth name
mBluetoothManager.setName(newName);
Scan a nearby Bluetooth device
mBluetoothManager.discovery();
Join a scan for Bluetooth device monitoring
mBluetoothManager.setOnDiscoveryDeviceListener(this);
/** * Start scanning callbacks for nearby Bluetooth devices */ @Override public void ondiscoverydevicestarted () {//TODO }/** * Scan to nearby Bluetooth device callback * * @param device bluetooth device */ @Override public void ondiscoverydevicefound (Bluetoothdevice device) {//TODO } /** * Scan nearby Bluetooth device complete callback */ @Override public void ondiscoverydevicefinished () {//TODO }
Remove the monitor for a scanned Bluetooth device
mBluetoothManager.removeOnDiscoveryDeviceListener();
Service-side initialization
new BluetoothService() { @Override protectedonSecureUuid() { // TODO 设置自己的UUID return UUID_SECURE; } @Override protectedonInsecureUuid() { // TODO 设置自己的UUID return UUID_INSECURE; }};
Waiting for client to connect
mBluetoothService.start();
Disconnecting/Freeing Resources
mBluetoothService.stop();
Join the Bluetooth connection for monitoring
Mbluetoothservice.setonserviceconnectlistener (NewOnserviceconnectlistener () {@Override Public void onconnectlistening() {//TODO}@Override Public void onconnectsuccess(Bluetoothdevice device) {//TODO}@Override Public void Onconnectfail(Exception e) {//TODO}@Override Public void Onconnectlost(Exception e) {//TODO}});
Send Message
mBluetoothService.send(chatText);
Join the Messaging listener
mBluetoothClient.setOnMessageListener(this);
/** * 蓝牙发送了消息 * * @param message 发送的消息 */@OverridepublicvoidonSend(String message) { // TODO}/** * 蓝牙接收到消息 * * @param message 接收的消息 */@OverridepublicvoidonRead(String message) { // TODO}
Client initialization
new BluetoothClient() { @Override protectedonSecureUuid() { // TODO 设置自己的UUID return UUID_SECURE; } @Override protectedonInsecureUuid() { // TODO 设置自己的UUID return UUID_INSECURE; }};
Bluetooth connection (Secure)
true);
Bluetooth connection (unsecured)
false);
Disconnecting/Freeing Resources
mBluetoothClient.stop();
Join the Bluetooth connection for monitoring
Mbluetoothclient.setonclientconnectlistener (NewOnclientconnectlistener () {@Override Public void onconnecting() {//TODO}@Override Public void onconnectsuccess(Bluetoothdevice device) {//TODO}@Override Public void Onconnectfail(Exception e) {//TODO}@Override Public void Onconnectlost(Exception e) {//TODO}});
Send Message
mBluetoothClient.send(chatText);
Join the Messaging listener
mBluetoothClient.setOnMessageListener(this);
/** * 蓝牙发送了消息 * * @param message 发送的消息 */@OverridepublicvoidonSend(String message) { // TODO}/** * 蓝牙接收到消息 * * @param message 接收的消息 */@OverridepublicvoidonRead(String message) { // TODO}
Android Bluetooth Communication--androidbluetoothmanager