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
Png
Gif
Basic functionality Add 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();
Add 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()
Modify Bluetooth name
mBluetoothManager.setName(newName);
Scan a nearby Bluetooth device
mBluetoothManager.discovery();
Add a scan for Bluetooth devices
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 for callbacks completed by nearby Bluetooth devices */ @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 connections
mBluetoothService.start();
Disconnecting/Freeing Resources
mBluetoothService.stop();
Add a listener for a Bluetooth connection
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);
Add a listener for messaging
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();
Add a listener for a Bluetooth connection
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);
Add a listener for messaging
mBluetoothClient.setOnMessageListener(this);
/** * 蓝牙发送了消息 * * @param message 发送的消息 */@OverridepublicvoidonSend(String message) { // TODO}/** * 蓝牙接收到消息 * * @param message 接收的消息 */@OverridepublicvoidonRead(String message) { // TODO}
Android Bluetooth Communication--androidbluetoothmanager