Saturday 27 June 2020

SCO and ACL in Bluetooth

Hi Guys !!! Hope all is well
I am going to discus Bluetooth ACL and SCO in Android.
  • ACL= Asynchronous Connection-Less
  • SCO = Synchronous Connection Oriented.
  • SCO is Point to Point Connection between only one master and only one slave
  • ACL is multi-point connection between one master and many slaves
SCO is for real-time narrow band signal which does not require retransmission
Voice data in Bluetooth hands free kit is an example for this
This narrow band audio is called "Bluetooth voice" type

Where as ACL is for all other BT data transfer including High quality audio/video data
Example is music playback through A2DP. This is high bandwidth data and hence called "Advanced Audio"


SCO is fixed bandwidth channels and can have maximum 3 channels per device,
but throughput of ACL varies with other active connections(SCO and ACL) at that time









Tuesday 23 June 2020

Bluetooth Low Energy Scanning

Hi Guys !!! Hope all is well
In my previous post  I have discussed BLE Advertisement. Now I am going to discus Bluetooth Low Energy (BLE) scanning in Android.

Recap from previous post
  • Bluetooth Low Energy (BLE), available in Android 4.3 and later
  • Creates short connections between devices to transfer bursts of data
  • Bluetooth Low Energy (BLE) conserves power by remaining in sleep mode most of the time
  • It wakes up only to make advertisements and short connections
This lets BLE provide lower bandwidth and reduced power consumption compared to Classic Bluetooth.
In this tutorial, We will learn about the BluetoothAdapter.LeScanCallback class,which enables developers to turn a supported phone into a Bluetooth LE scannerwithout the need for additional hardware. You can detect all near by BLE Beacons and communicate with them.
So  below sample code detect ble advertising devices(ble beacons) and list it down there name MAC Address and RSSI value. You can download complete project from my GitHub page 
/**
 * Start the bluetooth low energy scan.
*/
private boolean startBleScan() {
boolean isSuccess = true;
// Scan filter for iBeacon.
ScanFilter.Builder builder = new ScanFilter.Builder();
builder.setManufacturerData(0x004c, new byte[] {});
SCAN_FILTER_LIST.add(builder.build());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mScanCallback = new ScanCallback() {
@Override
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void onScanResult(final int callbackType,
final ScanResult result) {
// Print out the RSSI value from result
Log.d("saurabh_Tx", "onScanResult RSSI: "
+ result.getRssi());
try {
// Print out the rssi value
runOnUiThread(new Runnable() {
@Override
public void run() {
mDeviceAdapter.
update(result.getDevice(), result.getRssi(), 
result.getScanRecord());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onScanFailed(final int errorCode) {
Log.d("saurabh_ScanFailed", "errorCode: "
+ errorCode);
}
};
// There is no immediate value telling if scan is failed or not
if (null != mBTAdapter.getBluetoothLeScanner()) {
ScanSettings scanSettings = new ScanSettings.Builder().
setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
scanSettings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
.setNumOfMatches(ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT)
.build();
}
mBTAdapter.getBluetoothLeScanner().
startScan(SCAN_FILTER_LIST, scanSettings, mScanCallback);
} else {
isSuccess = false;
}
} else if (Build.VERSION.SDK_INT
>= Build.VERSION_CODES.JELLY_BEAN_MR2) {
// On some Android device, Bluetooth LE (Android Version < 4.1)
// scan is unstable. Turning off scan every 3 seconds as
// a workaround for this issue
mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device,
final int rssi, final byte[] scanRecord) {
try {
// Print out the rssi value
Log.d("saurabh_Tx", "onScanResult RSSI: " + rssi);
runOnUiThread(new Runnable() {
@Override
public void run() {
mDeviceAdapter.
update(device, rssi, scanRecord);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
};
isSuccess = mBTAdapter.startLeScan(mLeScanCallback);
}
return isSuccess;
}
Thanks
Saurabh 
Happy Coding !!!

Tuesday 9 June 2020

Bluetooth Low Energy Advertising

Hi Guys !!! Hope all is well
Today I am going to discus Bluetooth Low Energy (BLE) in Android.

  • Bluetooth Low Energy (BLE), available in Android 4.3 and later
  • Creates short connections between devices to transfer bursts of data
  • Bluetooth Low Energy (BLE) conserves power by remaining in sleep mode most of the time
  • It wakes up only to make advertisements and short connections
This lets BLE provide lower bandwidth and reduced power consumption compared to Classic Bluetooth.

In this tutorial, We will learn about the BluetoothLeAdvertiser class,
which enables developers to turn a supported phone into a Bluetooth LE beacon
without the need for additional hardware.

 



You can download complete project from my GitHub page 
    private void advertise() {
            mBluetoothAdvertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
            AdvertiseSettings settings = new AdvertiseSettings.Builder()
                    .setAdvertiseMode(mode)
                    .setTxPowerLevel(power)
                    .setTimeout(3*60*1000)
                    .setConnectable(true)
                    .build();

            ParcelUuid pUuid = new ParcelUuid( UUID.fromString( getString( R.string.ble_uuid ) ) );

            AdvertiseData data = new AdvertiseData.Builder()
                    .setIncludeDeviceName( true )
                    //.addServiceUuid( pUuid )           
          .addServiceData( pUuid, "SK".getBytes(Charset.forName("UTF-8") ) )
                    .build();

            advertisingCallback = new AdvertiseCallback() {
                @Override                
  public void onStartSuccess(AdvertiseSettings settingsInEffect) {
                    Log.e( "Saurabh_BLE", "Advertising onStartSuccess: getTxPowerLevel "                            
            + settingsInEffect.getTxPowerLevel() );
                    String modes = "";
                    if(2 == settingsInEffect.getMode()){
                        modes = "LOW LATENCY Mode";
                    }else if(1 == settingsInEffect.getMode()) {
                        modes = "BALANCED Mode";
                    }else if(0 == settingsInEffect.getMode()) {
                        modes = "LOW POWER Mode";
                    }

                    String power = "";
                    if(3 == settingsInEffect.getTxPowerLevel()){
                        power = "TX_POWER_HIGH";
                    }else if(2 == settingsInEffect.getTxPowerLevel()) {
                        power = "TX_POWER_MEDIUM";
                    }else if(1 == settingsInEffect.getTxPowerLevel()) {
                        power = "TX_POWER_LOW ";
                    } else if(0 == settingsInEffect.getTxPowerLevel()) {
                        power = "TX_POWER_ULTRA_LOW";
                    }

                    mText.setText("Advertising Start: Success \n"+
                            "Advertising PowerLevel: "+power+"\n"+
                            "Advertising Mode: "+modes+" \n"+
                            "Advertising Timeout(ms): "+settingsInEffect.getTimeout()+"\n"+
                            "Advertising Connectable: "+settingsInEffect.isConnectable());
                    super.onStartSuccess(settingsInEffect);
                }

                @Override                
             public void onStartFailure(int errorCode) {
                    Log.e( "Saurabh_BLE", "Advertising onStartFailure: " + errorCode );
                    mText.setText("Advertising Start Failure ErrorCode: "+errorCode);
                    super.onStartFailure(errorCode);
                }
            };

        mBluetoothAdvertiser.startAdvertising( settings, data, advertisingCallback );
    }


Thanks
Saurabh 
Happy Coding !!!

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...