在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):OmarAflak/Bluetooth-Library开源软件地址(OpenSource Url):https://github.com/OmarAflak/Bluetooth-Library开源编程语言(OpenSource Language):Java 100.0%开源软件介绍(OpenSource Introduction):Android Bluetooth Client LibraryThis is an Android library that simplifies the process of bluetooth communication, client side. This library does not support BLE devices; InstallAdd to your gradle dependencies:
Enable bluetoothCareful: You also have to enable phone location in newer versions of Android. Asking user for bluetooth activation@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// ...
// Need to ask for bluetooth permissions before calling constructor !
// Permissions are {BLUETOOTH, BLUETOOTH_ADMIN, ACCESS_COARSE_LOCATION}
bluetooth = new Bluetooth(this);
bluetooth.setBluetoothCallback(bluetoothCallback);
}
@Override
protected void onStart() {
super.onStart();
bluetooth.onStart();
if(bluetooth.isEnabled()){
// doStuffWhenBluetoothOn() ...
} else {
bluetooth.showEnableDialog(ScanActivity.this);
}
}
@Override
protected void onStop() {
super.onStop();
bluetooth.onStop();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
bluetooth.onActivityResult(requestCode, resultCode);
}
private BluetoothCallback bluetoothCallback = new BluetoothCallback() {
@Override public void onBluetoothTurningOn() {}
@Override public void onBluetoothTurningOff() {}
@Override public void onBluetoothOff() {}
@Override
public void onBluetoothOn() {
// doStuffWhenBluetoothOn() ...
}
@Override
public void onUserDeniedActivation() {
// handle activation denial...
}
}; Without asking user for bluetooth activation@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// ...
// Need to ask for bluetooth permissions before calling constructor !
// Permissions are {BLUETOOTH, BLUETOOTH_ADMIN, ACCESS_COARSE_LOCATION}
bluetooth = new Bluetooth(this);
bluetooth.setBluetoothCallback(bluetoothCallback);
}
@Override
protected void onStart() {
super.onStart();
bluetooth.onStart();
if(bluetooth.isEnabled()){
// doStuffWhenBluetoothOn() ...
} else {
bluetooth.enable()
}
}
@Override
protected void onStop() {
super.onStop();
bluetooth.onStop();
}
private BluetoothCallback bluetoothCallback = new BluetoothCallback() {
@Override public void onBluetoothTurningOn() {}
@Override public void onBluetoothTurningOff() {}
@Override public void onBluetoothOff() {}
@Override public void onUserDeniedActivation() {}
@Override
public void onBluetoothOn() {
// doStuffWhenBluetoothOn() ...
}
}; Discover devices and pairListenerbluetooth.setDiscoveryCallback(new DiscoveryCallback() {
@Override public void onDiscoveryStarted() {}
@Override public void onDiscoveryFinished() {}
@Override public void onDeviceFound(BluetoothDevice device) {}
@Override public void onDevicePaired(BluetoothDevice device) {}
@Override public void onDeviceUnpaired(BluetoothDevice device) {}
@Override public void onError(String message) {}
}); Scan and Pairbluetooth.startScanning();
bluetooth.pair(device);
bluetooth.pair(device, "optional pin"); Get paired devicesList<BluetoothDevice> devices = bluetooth.getPairedDevices(); Connect to device and communicateListenerbluetooth.setDeviceCallback(new DeviceCallback() {
@Override public void onDeviceConnected(BluetoothDevice device) {}
@Override public void onDeviceDisconnected(BluetoothDevice device, String message) {}
@Override public void onMessage(byte[] message) {}
@Override public void onError(String message) {}
@Override public void onConnectError(BluetoothDevice device, String message) {}
}); Connect to device// three options
bluetooth.connectToName("name");
bluetooth.connectToAddress("address");
bluetooth.connectToDevice(device); Connect to device using port trickSee this post for details: https://stackoverflow.com/a/25647197/5552022 bluetooth.connectToNameWithPortTrick("name");
bluetooth.connectToAddressWithPortTrick("address");
bluetooth.connectToDeviceWithPortTrick(device); Should be avoided Send a messagebluetooth.send("hello, world");
bluetooth.send(new byte[]{61, 62, 63}); Receive messagesThe default behavior of the library is the read from the input stream until it hits a new line, it will then propagate the message through listeners as a byte array.
You can change the way the library reads from the socket by creating your own reader class. It must extend from The default behavior is actually one example of implementation : public class LineReader extends SocketReader{
private BufferedReader reader;
public LineReader(InputStream inputStream) {
super(inputStream);
reader = new BufferedReader(new InputStreamReader(inputStream));
}
@Override
public byte[] read() throws IOException {
return reader.readLine().getBytes();
}
} This is an implementation for a custom delimiter : public class DelimiterReader extends SocketReader {
private PushbackInputStream reader;
private byte delimiter;
public DelimiterReader(InputStream inputStream) {
super(inputStream);
reader = new PushbackInputStream(inputStream);
delimiter = 0;
}
@Override
public byte[] read() throws IOException {
List<Byte> byteList = new ArrayList<>();
byte[] tmp = new byte[1];
while(true) {
int n = reader.read();
reader.unread(n);
int count = reader.read(tmp);
if(count > 0) {
if(tmp[0] == delimiter){
byte[] returnBytes = new byte[byteList.size()];
for(int i=0 ; i<byteList.size() ; i++){
returnBytes[i] = byteList.get(i);
}
return returnBytes;
} else {
byteList.add(tmp[0]);
}
}
}
}
} Then you can use your reader : bluetooth.setReader(LineReader.class); JavaDocJavaDoc is in Example CodeDownload Demo AppLicense
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论