Created
December 23, 2019 08:47
-
-
Save cyberrob-zz/b1fd973d6d6d06ab5b862fb52bbd9ec6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.noodoe.sunray.cmu.library; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Build; | |
import com.invisibi.iv01.cmu.utils.DLog; | |
import com.invisibi.iv01.cmu.utils.Helpers; | |
import com.noodoe.fwk.ble.v02.service.connection.NDBTService; | |
import com.noodoe.fwk.ble.v02.utils.CMUConstants; | |
import com.noodoe.fwk.ble.v02.utils.ServiceConfiguration; | |
import com.noodoe.fwk.ble.v02.utils.db.DeviceContentUtils; | |
import com.noodoe.sunray.cmu.SunrayBleAdapter; | |
import com.noodoe.sunray.cmu.service.connection.SunrayBTService; | |
/** | |
* Created by emilychen07 on 7/29/16. | |
*/ | |
public class SunrayConnectionLibrary { | |
public final static String TAG = SunrayConnectionLibrary.class.getSimpleName(); | |
public static boolean startBluetoothService(Context context) { | |
//start Sunray Bluetooth Service if there is device linked | |
String linkedDevice = SunrayBleAdapter.getLinkedDevice(context); | |
if (!Helpers.isEmptyString(linkedDevice)) { | |
Intent intent = new Intent(context, SunrayBTService.class); | |
intent.putExtra(NDBTService.SERVICE_CONFIG_EXTRA_NAME, | |
new ServiceConfiguration(ServiceConfiguration.SERVICE_TYPE.SUNRAY, true/*isContinuousConnectionService*/)); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
context.startForegroundService(intent); | |
} else { | |
context.startService(intent); | |
} | |
DLog.d(TAG, String.format("Start SunrayBTService for linked device: %s", linkedDevice)); | |
return true; | |
} | |
return false; | |
} | |
public static boolean requestConnectionStatus(Context context) { | |
if (context == null) { | |
DLog.w(TAG, "invalid arguments; context null"); | |
return false; | |
} | |
Intent queryIntent = new Intent(context, SunrayBTService.class); | |
queryIntent.putExtra(SunrayBTService.REQUEST_STATUS_EXTRA_NAME, true); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
context.startForegroundService(queryIntent); | |
} else { | |
context.startService(queryIntent); | |
} | |
return true; | |
} | |
/** | |
* link to a specific device | |
* | |
* @param context the context of the caller | |
* @param deviceName the device to be linked | |
* @param deviceAddress the device to be linked | |
* @return task successfully done or not | |
*/ | |
public static boolean linkToDevice(Context context, String deviceName, String deviceAddress) { | |
if (context == null) { | |
DLog.e(TAG, "invalid arguments; context null"); | |
return false; | |
} | |
if (Helpers.isEmptyString(deviceAddress)) { | |
DLog.e(TAG, "invalid arguments; device null"); | |
return false; | |
} | |
SunrayBleAdapter.unlinkPreviousDevice(context); | |
DeviceContentUtils.unlinkPreviousDevice(context, TAG); | |
DeviceContentUtils | |
.linkToDevice(context, deviceAddress, deviceName, CMUConstants.GattServiceType.creation, | |
TAG); | |
SunrayBleAdapter.connectDevice(context); | |
return true; | |
} | |
/** | |
* link to a specific device | |
* | |
* @param context the context of the caller | |
* @param deviceName the device to be linked | |
* @param deviceAddress the device to be linked | |
* @return task successfully done or not | |
*/ | |
public static boolean linkToDeviceWithoutConnect(Context context, String deviceName, String deviceAddress) { | |
if (context == null) { | |
DLog.e(TAG, "invalid arguments; context null"); | |
return false; | |
} | |
if (Helpers.isEmptyString(deviceAddress)) { | |
DLog.e(TAG, "invalid arguments; device null"); | |
return false; | |
} | |
SunrayBleAdapter.unlinkPreviousDevice(context); | |
DeviceContentUtils.unlinkPreviousDevice(context, TAG); | |
DeviceContentUtils | |
.linkToDevice(context, deviceAddress, deviceName, CMUConstants.GattServiceType.creation, | |
TAG); | |
return true; | |
} | |
/** | |
* unlink to the specific device | |
* | |
* @param context the context of the caller | |
* @param deviceAddress the target device address | |
* @return task successfully done or not | |
*/ | |
public static boolean unlinkToDevice(Context context, String deviceAddress) { | |
if (context == null) { | |
DLog.e(TAG, "invalid arguments; context null"); | |
return false; | |
} | |
if (Helpers.isEmptyString(deviceAddress)) { | |
DLog.e(TAG, "invalid arguments; deviceAddress null"); | |
return false; | |
} | |
SunrayBleAdapter.unlinkPreviousDevice(context); | |
DeviceContentUtils.unlinkPreviousDevice(context, TAG); | |
SunrayBleAdapter.disconnectDevice(context); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment