Friday, July 27, 2018
Lenovo 3000 G360 Windows 7 32it 64Bit Driver
Lenovo 3000 G360 Windows 7 32it 64Bit Driver
I demonstrate how to write a simple BLE peripheral application in Android here. I am bad in Android development, The UI would be very ugly, but the code work:
Currently(5/25/2015), the code could be running in Nexus 6 or Nexus 9 only based on my test. The other phones or tablets DO NOT support to be a BLE peripheral. So, if you really interested in the issue of Android as BLE Peripheral , please open your wallet or swipe your card, to buy a GOOGLE official device, thank you.
To add a characteristic as notification is little bit complicated, In here I just add read and write characteristics.
About the notification, I put code in the last part of this post.
You should add
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
The 2 lines in your AndroidManifest.xml, like this :
<?xml version="1.0" encoding="utf-8"?>
<manifest >"http://schemas.android.com/apk/res/android"
package="com.gaiger.simplebleperipheral"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The kernal code are below , note the AdvertiseCallback is callback of BluetoothLeAdvertiser ::startAdvertising, and BluetoothGattServerCallback is callback function of ALL BluetoothGattCharacteristic.
BLEPeripheral.java: (that is what you want)
package com.gaiger.simplebleperipheral;
import java.util.List;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattServer;
import android.bluetooth.BluetoothGattServerCallback;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.util.Log;
public class BLEPeripheral{
public interface ConnectionCallback {
void onConnectionStateChange(BluetoothDevice device, int newState);
}
BluetoothManager mManager;
BluetoothAdapter mAdapter;
BluetoothLeAdvertiser mLeAdvertiser;
AdvertiseSettings.Builder settingBuilder;
AdvertiseData.Builder advBuilder;
BluetoothGattServer mGattServer;
ConnectionCallback mConnectionCallback;
public interface WriteCallback {
void onWrite(byte[] data);
}
WriteCallback mWriteCallback;
public static boolean isEnableBluetooth(){
return BluetoothAdapter.getDefaultAdapter().isEnabled();
}
public int init(Context context){
if(null == mManager)
{
mManager = (BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE);
if(null == mManager)
return -1;
if(false == context.getPackageManager().
hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
return -2;
}
if(null == mAdapter)
{
mAdapter = mManager.getAdapter();
if(false == mAdapter.isMultipleAdvertisementSupported())
return -3;
}
if(null == settingBuilder)
{
settingBuilder = new AdvertiseSettings.Builder();
settingBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY);
settingBuilder.setConnectable(true);
settingBuilder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);
}
if(null == advBuilder)
{
advBuilder = new AdvertiseData.Builder();
mAdapter.setName("SimplePeripheral");
advBuilder.setIncludeDeviceName(true);
}
if(null == mGattServer)
{
mGattServer = mManager.openGattServer(context, mGattServerCallback);
if(null == mGattServer)
return -4;
addDeviceInfoService();
}
return 0;
}
public void setConnectionCallback(ConnectionCallback callback)
{
mConnectionCallback = callback;
}
public void close()
{
if(null != mLeAdvertiser)
stopAdvertise();
if(null != mGattServer)
mGattServer.close();
mGattServer = null;
if(null != advBuilder)
advBuilder = null;
if(null != settingBuilder)
settingBuilder = null;
if(null != mAdapter)
mAdapter = null;
if(null != mManager)
mManager = null;
}
public static String getAddress(){return BluetoothAdapter.getDefaultAdapter().getAddress();}
private AdvertiseCallback mAdvCallback = new AdvertiseCallback() {
@Override
public void onStartFailure(int errorCode){
Log.d("advertise","onStartFailure");
}
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect){
Log.d("advertise","onStartSuccess");
};
};
private final BluetoothGattServerCallback mGattServerCallback
= new BluetoothGattServerCallback(){
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState){
Log.d("GattServer", "Our gatt server connection state changed, new state ");
Log.d("GattServer", Integer.toString(newState));
if(null != mConnectionCallback && BluetoothGatt.GATT_SUCCESS == status)
mConnectionCallback.onConnectionStateChange(device, newState);
super.onConnectionStateChange(device, status, newState);
}
@Override
public void onServiceAdded(int status, BluetoothGattService service) {
Log.d("GattServer", "Our gatt server service was added.");
super.onServiceAdded(status, service);
}
@Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristicvisit link download