Skip to content

Commit

Permalink
feat(java): add basic scanning
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <sfroment42@gmail.com>
  • Loading branch information
sfroment committed Nov 9, 2018
1 parent bdae640 commit 11a7db6
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 4 deletions.
@@ -0,0 +1,17 @@
package chat.berty.ble;

import android.bluetooth.BluetoothDevice;

public class BertyDevice {

public String addr;
public String peerID;
public String ma;
public BluetoothDevice device;

public BertyDevice(BluetoothDevice rDevice, String address) {
addr = address;
device = rDevice;
}

}
Expand Up @@ -35,7 +35,10 @@

import android.Manifest;

import java.lang.reflect.Array;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -72,6 +75,8 @@ public class Manager {

public static String TAG = "chat.berty.ble.Manager";

protected HashMap<String, BertyDevice> bertyDevices;

protected BluetoothAdapter mBluetoothAdapter;

protected BluetoothGattServer mBluetoothGattServer;
Expand Down Expand Up @@ -115,6 +120,7 @@ public static Manager getInstance() {
if (instance == null) {
instance = new Manager();
instance.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
instance.bertyDevices = new HashMap<>();
}
}
}
Expand Down Expand Up @@ -156,6 +162,7 @@ public AdvertiseData makeAdvertiseData() {
.setIncludeDeviceName(true)
.setIncludeTxPowerLevel(false)
.addServiceUuid(pUuid);

return builder.build();
}
return null;
Expand Down Expand Up @@ -271,13 +278,109 @@ public void onStartFailure(int errorCode) {
}
}

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;

public @Nullable ScanSettings createScanSetting() {
ScanSettings settings = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
}
return settings;
}

public @Nullable ScanFilter makeFilter() {
ParcelUuid pUuid = new ParcelUuid(SERVICE_UUID);
ScanFilter filter = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
filter = new ScanFilter.Builder().setServiceUuid(pUuid).build();

}
return filter;
}

private ScanCallback mScanCallback;

public void initScanCallBack() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mScanCallback = new ScanCallback() {
public void parseResult(ScanResult result) {
BluetoothDevice device = result.getDevice();
String addr = device.getAddress();
synchronized (bertyDevices) {
if (!bertyDevices.containsKey(addr)) {
BertyDevice bDevice = new BertyDevice(device, addr);
bertyDevices.put(addr, bDevice);
device.connectGatt(mContext, false, mGattCallback);
Log.e(TAG, "connecting to " + addr);
}
}
}

@Override
public void onScanResult(int callbackType, ScanResult result) {
parseResult(result);
super.onScanResult(callbackType, result);
}

@Override
public void onBatchScanResults(List<ScanResult> results) {
for (ScanResult result:results) {
parseResult(result);
}
super.onBatchScanResults(results);
}

@Override
public void onScanFailed(int errorCode) {
String errorString;

switch(errorCode) {
case SCAN_FAILED_ALREADY_STARTED: errorString = "SCAN_FAILED_ALREADY_STARTED";
break;

case SCAN_FAILED_APPLICATION_REGISTRATION_FAILED: errorString = "SCAN_FAILED_APPLICATION_REGISTRATION_FAILED";
break;

case SCAN_FAILED_INTERNAL_ERROR: errorString = "SCAN_FAILED_INTERNAL_ERROR";
break;

case SCAN_FAILED_FEATURE_UNSUPPORTED: errorString = "SCAN_FAILED_FEATURE_UNSUPPORTED";
break;

default: errorString = "UNKNOW FAIL";
break;
}
Log.e(TAG, "error scanning " + errorString);
super.onScanFailed(errorCode);
}
};
}
}

public void startAdvertising() {
AdvertiseSettings settings = createAdvSettings(true, 0);
AdvertiseData advData = makeAdvertiseData();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBluetoothLeAdvertiser.startAdvertising(settings, advData, mAdvertisingCallback);
}
}

public void startScanning() {
ScanSettings settings = createScanSetting();
ScanFilter filter = makeFilter();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBluetoothLeScanner.startScan(Arrays.asList(filter), settings, mScanCallback);
}
}

public String realTest() {
Log.e(TAG, "THIS IS REAL SHIT GUY");
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Activity curActivity = mReactContext.getCurrentActivity();
if (!mBluetoothAdapter.isEnabled()) {
Log.e(TAG, "NEED TO ENABLE");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
curActivity.startActivityForResult(enableBtIntent, BLUETOOTH_ENABLE_REQUEST);
}
Expand All @@ -286,6 +389,8 @@ public String realTest() {
initGattServerCallBack();
initGattCallback();
initAdvertiseCallback();
initScanCallBack();

BluetoothManager mb = (BluetoothManager) mContext.getSystemService(BLUETOOTH_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
Expand All @@ -294,11 +399,78 @@ public String realTest() {
mBluetoothGattServer = mb.openGattServer(mContext, mGattServerCallback);
mBluetoothGattServer.addService(createService());

AdvertiseSettings settings = createAdvSettings(true, 0);
AdvertiseData advData = makeAdvertiseData();
mBluetoothLeAdvertiser.startAdvertising(settings, advData, mAdvertisingCallback);
// startAdvertising();
startScanning();
}
}
return "COMING FROM THAT MOTHA FUCKING JAVA";
}

protected BluetoothGattCallback mGattCallback;

public void initGattCallback() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mGattCallback = new BluetoothGattCallback() {
@Override
public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
super.onPhyUpdate(gatt, txPhy, rxPhy, status);
}

@Override
public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
super.onPhyRead(gatt, txPhy, rxPhy, status);
}

@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
}

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
}

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
}

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
}

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
}

@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
}

@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
}

@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
super.onReliableWriteCompleted(gatt, status);
}

@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
}

@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
}
};
}
}
}

0 comments on commit 11a7db6

Please sign in to comment.