本文整理汇总了Java中android.bluetooth.BluetoothManager.openGattServer方法的典型用法代码示例。如果您正苦于以下问题:Java BluetoothManager.openGattServer方法的具体用法?Java BluetoothManager.openGattServer怎么用?Java BluetoothManager.openGattServer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.bluetooth.BluetoothManager
的用法示例。
在下文中一共展示了BluetoothManager.openGattServer方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: startServer
import android.bluetooth.BluetoothManager; //导入方法依赖的package包/类
/**
* Start the CurrentTimeService GATT server
* @return true if the GATT server starts successfully or is already running
*/
public static boolean startServer(Context context) {
if (sGattServer == null) {
BluetoothManager manager = (BluetoothManager) context.getSystemService(BLUETOOTH_SERVICE);
CurrentTimeCallback callback = new CurrentTimeCallback();
sGattServer = manager.openGattServer(context, callback);
if (sGattServer == null) {
Log.e(TAG, "Unable to start GATT server");
return false;
}
sGattServer.addService(GATT_SERVICE);
callback.setGattServer(sGattServer);
} else {
Log.w(TAG, "Already started");
}
return true;
}
示例2: GattService
import android.bluetooth.BluetoothManager; //导入方法依赖的package包/类
public GattService(AbstractLicenseActivity activity, BluetoothManager bluetoothManager) throws RemoteConnectionException {
Log.d(TAG, "Create " + this.toString() + " for activity " + activity.toString());
this.activity = activity;
gattServer = bluetoothManager.openGattServer(activity, this);
if (gattServer == null) {
throw new RemoteConnectionException("Could not create GattServer", false);
}
bluetoothGattService = new BluetoothGattService(
Constants.SERVICE_UUID,
BluetoothGattService.SERVICE_TYPE_PRIMARY
);
bluetoothGattService.addCharacteristic(apduCharacteristic);
}
示例3: GattServer
import android.bluetooth.BluetoothManager; //导入方法依赖的package包/类
public GattServer(Context context){
mContext = context;
mBluetoothManager = (BluetoothManager) mContext.getSystemService(BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
mConnectedDevices = new ArrayList<>();
mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
mGattServer = mBluetoothManager.openGattServer(mContext, mGattServerCallback);
initServer();
startAdvertising();
}
示例4: onCreate
import android.bluetooth.BluetoothManager; //导入方法依赖的package包/类
@Override
public void onCreate() {
super.onCreate();
BluetoothManager man = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
mGattServer = man.openGattServer(this, mGattCallback);
mGattServer.addService(mU2FGattService);
mBtLeAdvertiser = man.getAdapter().getBluetoothLeAdvertiser();
mBtLeAdvertiser.startAdvertising(cfg, data, cb);
}
示例5: openGattServer
import android.bluetooth.BluetoothManager; //导入方法依赖的package包/类
private void openGattServer(Context context, BluetoothManager manager) {
mBluetoothGattServer = manager.openGattServer(context, mGattServerCallbacks);
}
示例6: HidPeripheral
import android.bluetooth.BluetoothManager; //导入方法依赖的package包/类
/**
* Constructor<br />
* Before constructing the instance, check the Bluetooth availability.
*
* @param context the ApplicationContext
* @param needInputReport true: serves 'Input Report' BLE characteristic
* @param needOutputReport true: serves 'Output Report' BLE characteristic
* @param needFeatureReport true: serves 'Feature Report' BLE characteristic
* @param dataSendingRate sending rate in milliseconds
* @throws UnsupportedOperationException if starting Bluetooth LE Peripheral failed
*/
protected HidPeripheral(final Context context, final boolean needInputReport, final boolean needOutputReport, final boolean needFeatureReport, final int dataSendingRate) throws UnsupportedOperationException {
applicationContext = context.getApplicationContext();
handler = new Handler(applicationContext.getMainLooper());
final BluetoothManager bluetoothManager = (BluetoothManager) applicationContext.getSystemService(Context.BLUETOOTH_SERVICE);
final BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter == null) {
throw new UnsupportedOperationException("Bluetooth is not available.");
}
if (!bluetoothAdapter.isEnabled()) {
throw new UnsupportedOperationException("Bluetooth is disabled.");
}
Log.d(TAG, "isMultipleAdvertisementSupported:" + bluetoothAdapter.isMultipleAdvertisementSupported());
if (!bluetoothAdapter.isMultipleAdvertisementSupported()) {
throw new UnsupportedOperationException("Bluetooth LE Advertising not supported on this device.");
}
bluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
Log.d(TAG, "bluetoothLeAdvertiser: " + bluetoothLeAdvertiser);
if (bluetoothLeAdvertiser == null) {
throw new UnsupportedOperationException("Bluetooth LE Advertising not supported on this device.");
}
gattServer = bluetoothManager.openGattServer(applicationContext, gattServerCallback);
if (gattServer == null) {
throw new UnsupportedOperationException("gattServer is null, check Bluetooth is ON.");
}
// setup services
addService(setUpHidService(needInputReport, needOutputReport, needFeatureReport));
addService(setUpDeviceInformationService());
addService(setUpBatteryService());
// send report each dataSendingRate, if data available
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
final byte[] polled = inputReportQueue.poll();
if (polled != null && inputReportCharacteristic != null) {
inputReportCharacteristic.setValue(polled);
handler.post(new Runnable() {
@Override
public void run() {
final Set<BluetoothDevice> devices = getDevices();
for (final BluetoothDevice device : devices) {
try {
if (gattServer != null) {
gattServer.notifyCharacteristicChanged(device, inputReportCharacteristic, false);
}
} catch (final Throwable ignored) {
}
}
}
});
}
}
}, 0, dataSendingRate);
}
示例7: onStart
import android.bluetooth.BluetoothManager; //导入方法依赖的package包/类
@Override
public void onStart() {
final BluetoothManager bluetoothManager =
(BluetoothManager) getContext().getSystemService(Context.BLUETOOTH_SERVICE);
synchronized (bluetoothManager) {
bluetoothGattServer = bluetoothManager.openGattServer(getContext(), new ServerCallback());
final BluetoothGattCharacteristic writeCharacteristic1 = new BluetoothGattCharacteristic(
Constants.BLUETOOTH_WRITE_CHARACTERISTIC_UUID,
BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE,
BluetoothGattCharacteristic.PERMISSION_WRITE);
final BluetoothGattCharacteristic readCharacteristic1 = new BluetoothGattCharacteristic(
Constants.BLUETOOTH_READ_CHARACTERISTIC_UUID,
BluetoothGattCharacteristic.PROPERTY_READ,
BluetoothGattCharacteristic.PERMISSION_READ);
final BluetoothGattCharacteristic writeCharacteristic2 = new BluetoothGattCharacteristic(
Constants.BLUETOOTH_WRITE_CHARACTERISTIC_UUID,
BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE,
BluetoothGattCharacteristic.PERMISSION_WRITE);
final BluetoothGattCharacteristic readCharacteristic2 = new BluetoothGattCharacteristic(
Constants.BLUETOOTH_READ_CHARACTERISTIC_UUID,
BluetoothGattCharacteristic.PROPERTY_READ,
BluetoothGattCharacteristic.PERMISSION_READ);
final BluetoothGattService bluetoothGattService = new BluetoothGattService(
Constants.BLUETOOTH_SERVICE_UUID,
BluetoothGattService.SERVICE_TYPE_PRIMARY);
bluetoothGattService.addCharacteristic(writeCharacteristic1);
bluetoothGattService.addCharacteristic(readCharacteristic1);
final UUID pubKeyUuid = UUID.nameUUIDFromBytes(getWalletServiceBinder().getMultisigClientKey().getPubKey());
Log.d(TAG, "pub key: " + pubKeyUuid);
final BluetoothGattService bluetoothPubKeyGattService = new BluetoothGattService(
pubKeyUuid, BluetoothGattService.SERVICE_TYPE_PRIMARY);
if (!bluetoothPubKeyGattService.addCharacteristic(writeCharacteristic2)) {
Log.d(TAG, "could not add write characteristic: " + writeCharacteristic2);
}
if (!bluetoothPubKeyGattService.addCharacteristic(readCharacteristic2)) {
Log.d(TAG, "could not add read characteristic: " + readCharacteristic2);
}
//bluetoothGattService.addService(bluetoothPubKeyGattService);
if (!bluetoothGattServer.addService(bluetoothGattService)) {
Log.d(TAG, "could not add service1: " + bluetoothGattService);
}
//hack, throws null pointer when adding a second service, but only on the nexus 9
while(true) {
try {
if (!bluetoothGattServer.addService(bluetoothPubKeyGattService)) {
Log.d(TAG, "could not add service2: " + bluetoothGattService);
} else {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
Log.d(TAG, "Bluetooth LE Default Service UUID: " + Constants.BLUETOOTH_SERVICE_UUID.toString());
Log.d(TAG, "Bluetooth LE PubKey Service UUID: " + pubKeyUuid.toString());
startAdvertisingService(Constants.BLUETOOTH_SERVICE_UUID);
startAdvertisingService(pubKeyUuid);
}
}
示例8: start
import android.bluetooth.BluetoothManager; //导入方法依赖的package包/类
/**
* Atempts to add this GATT service to the device's GATT server.
* @param beacon The initial beacon that will become connectable and be presented as configured currently.
* @return True if the GATT service was successfully added to the device's Bluetooth GATT server.
* Only one GATT service can run on the same device at the same time.
*/
public boolean start(@NonNull EddystoneBase beacon) {
if (mStarted) return false;
mBeacon = beacon;
Context context = Beacons.getContext();
mBluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
if (null == mBluetoothManager) {
// this check mostly hides a NPE warning - this is not null on Pixel 2 API 27 emulator
log("Could not obtain access to Bluetooth manager");
return false;
}
// fix an inner NPE not handled by openGattServer
if (null == mBluetoothManager.getAdapter()) {
log("No Bluetooth adapter");
return false;
}
mGattServer = mBluetoothManager.openGattServer(context, this);
if (null == mGattServer) {
log("Failed to open GATT server");
return false;
}
List<BluetoothGattService> gattServices = mGattServer.getServices();
for (BluetoothGattService service : gattServices) {
if (service.getUuid() == EddystoneGattService.UUID_EDDYSTONE_GATT_SERVICE) {
log("Another Eddystone-GATT service is already being served by this device");
close();
return false;
}
}
mEddystoneConfigurator = new EddystoneGattConfigurator(beacon);
mEddystoneGattService = new EddystoneGattService(this, mEddystoneConfigurator);
if (!mGattServer.addService(mEddystoneGattService.getService())) {
log("Eddystone-GATT service registration failed");
close();
return false;
}
// advertise beacon as connectable
if (!beacon.isConnectable()) {
log("Setting beacon connectable");
beacon.edit().setConnectable(true).apply();
}
// finally, make sure the provided beacon is started
return beacon.start();
}
示例9: getGattServer
import android.bluetooth.BluetoothManager; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private BluetoothGattServer getGattServer(Context context, BluetoothManager manager) {
return manager.openGattServer(context, mBLEServerAdaptor);
}