本文整理匯總了Java中android.bluetooth.BluetoothManager類的典型用法代碼示例。如果您正苦於以下問題:Java BluetoothManager類的具體用法?Java BluetoothManager怎麽用?Java BluetoothManager使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
BluetoothManager類屬於android.bluetooth包,在下文中一共展示了BluetoothManager類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: onCreate
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// initialize the right scan callback for the current API level
if (Build.VERSION.SDK_INT >= 21) {
mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
mRecyclerViewAdapter.addDevice(result.getDevice().getAddress());
}
};
} else {
mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
mRecyclerViewAdapter.addDevice(bluetoothDevice.getAddress());
}
};
}
// initialize bluetooth manager & adapter
BluetoothManager manager = (BluetoothManager) getActivity().getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = manager.getAdapter();
}
示例2: onCreate
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We set the content View of this Activity
setContentView(R.layout.activity_main);
// Get all the TextViews
deviceAddressTextView = (TextView) findViewById(R.id.device_address);
actionTextView = (TextView) findViewById(R.id.action);
rssiTextView = (TextView) findViewById(R.id.rssi);
// Get the descriptions of the actions
actionDescriptions = getResources().getStringArray(R.array.action_descriptions);
// Get the BluetoothManager so we can get the BluetoothAdapter
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
}
示例3: onCreate
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
// mBluetoothAdapterの取得
mBluetoothAdapter = bluetoothManager.getAdapter();
// mBluetoothLeScannerの初期化
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
Uri.Builder builder = new Uri.Builder();
AsyncHttpRequest task = new AsyncHttpRequest(this);
task.execute(builder);
scan(true);
}
示例4: setupBLE
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
private void setupBLE() {
if (!isBLESupported(this)) {
Toast.makeText(this, "device not support ble", Toast.LENGTH_SHORT).show();
finish();
return;
}
BluetoothManager manager = getManager(this);
if (manager != null) {
mBTAdapter = manager.getAdapter();
}
if ((mBTAdapter == null) || (!mBTAdapter.isEnabled())) {
Toast.makeText(this, "bluetooth not open", Toast.LENGTH_SHORT).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
return;
}
}
示例5: checkBluetooth
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
static public boolean checkBluetooth(Context context){
BluetoothManager bm = (BluetoothManager) context.getSystemService(BLUETOOTH_SERVICE);
BluetoothAdapter ba = bm.getAdapter();
if (ba == null) {
//Bluetooth is disabled
Log.e(TAG, "BluetoothAdapter not available!");
return false;
}
if(!ba.isEnabled()) {
Log.w(TAG, "BluetoothAdapter not enabled!");
ba.enable();
}
if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Log.e(TAG, "Bluetooth LE is not supported");
return false;
}
if(!ba.isMultipleAdvertisementSupported()){
Log.i(TAG, "No Multiple Advertisement Support!");
}
return ba.isEnabled();
}
示例6: checkBluetooth
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
public BLEClient.BtError checkBluetooth(){
btManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
btAdapter = btManager.getAdapter();
if(!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH))
return BLEClient.BtError.NoBluetooth;
if(!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
return BLEClient.BtError.NoBLE;
if(btAdapter == null || !btAdapter.isEnabled())
return BLEClient.BtError.Disabled;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && useNewMethod){
if((ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) &&
(ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED)){
return BtError.NoLocationPermission;
}
LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
if(!(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) && !(lm.isProviderEnabled(LocationManager.GPS_PROVIDER))){
return BtError.LocationDisabled;
}
}
return BLEClient.BtError.None;
}
示例7: onCreate
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
public void onCreate(Context context, GattServerListener listener) throws RuntimeException {
mContext = context;
mListener = listener;
mBluetoothManager = (BluetoothManager) context.getSystemService(BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = mBluetoothManager.getAdapter();
if (!checkBluetoothSupport(bluetoothAdapter)) {
throw new RuntimeException("GATT server requires Bluetooth support");
}
// Register for system Bluetooth events
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
mContext.registerReceiver(mBluetoothReceiver, filter);
if (!bluetoothAdapter.isEnabled()) {
Log.d(TAG, "Bluetooth is currently disabled... enabling");
bluetoothAdapter.enable();
} else {
Log.d(TAG, "Bluetooth enabled... starting services");
startAdvertising();
startServer();
}
}
示例8: prepareForScan
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
private void prepareForScan() {
if (isBleSupported()) {
// Ensures Bluetooth is enabled on the device
BluetoothManager btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter btAdapter = btManager.getAdapter();
if (btAdapter.isEnabled()) {
// Prompt for runtime permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
startLeScan();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION_LOCATION);
}
} else {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
} else {
Toast.makeText(this, "BLE is not supported", Toast.LENGTH_LONG).show();
finish();
}
}
示例9: onCreate
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
public void onCreate(Context context, String deviceAddress, OnCounterReadListener listener) throws RuntimeException {
mContext = context;
mListener = listener;
mDeviceAddress = deviceAddress;
mBluetoothManager = (BluetoothManager) context.getSystemService(BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
if (!checkBluetoothSupport(mBluetoothAdapter)) {
throw new RuntimeException("GATT client requires Bluetooth support");
}
// Register for system Bluetooth events
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
mContext.registerReceiver(mBluetoothReceiver, filter);
if (!mBluetoothAdapter.isEnabled()) {
Log.w(TAG, "Bluetooth is currently disabled... enabling");
mBluetoothAdapter.enable();
} else {
Log.i(TAG, "Bluetooth enabled... starting client");
startClient();
}
}
示例10: onStartCommand
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
if (intent == null || !intent.hasExtra(EXTRA_DEVICE_ADDRESS))
throw new UnsupportedOperationException("No device address at EXTRA_DEVICE_ADDRESS key");
mDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);
// notify user about changing the state to CONNECTING
final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE);
broadcast.putExtra(EXTRA_CONNECTION_STATE, STATE_CONNECTING);
LocalBroadcastManager.getInstance(BleProfileService.this).sendBroadcast(broadcast);
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
final BluetoothAdapter adapter = bluetoothManager.getAdapter();
final BluetoothDevice device = adapter.getRemoteDevice(mDeviceAddress);
mDeviceName = device.getName();
mBleManager.connect(device);
return START_REDELIVER_INTENT;
}
示例11: onStartCommand
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
if (intent == null || !intent.hasExtra(EXTRA_DEVICE_ADDRESS))
throw new UnsupportedOperationException("No device address at EXTRA_DEVICE_ADDRESS key");
final Uri logUri = intent.getParcelableExtra(EXTRA_LOG_URI);
mLogSession = Logger.openSession(getApplicationContext(), logUri);
mDeviceAddress = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);
Logger.i(mLogSession, "Service started");
// notify user about changing the state to CONNECTING
final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE);
broadcast.putExtra(EXTRA_CONNECTION_STATE, STATE_CONNECTING);
LocalBroadcastManager.getInstance(BleProfileService.this).sendBroadcast(broadcast);
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
final BluetoothAdapter adapter = bluetoothManager.getAdapter();
final BluetoothDevice device = adapter.getRemoteDevice(mDeviceAddress);
mDeviceName = device.getName();
onServiceStarted();
mBleManager.connect(device);
return START_REDELIVER_INTENT;
}
示例12: connect
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
@Override
public void connect(final BluetoothDevice device) {
// Should we use the GATT Server?
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
final boolean useGattServer = preferences.getBoolean(ProximityActivity.PREFS_GATT_SERVER_ENABLED, true);
if (useGattServer) {
// Save the device that we want to connect to. First we will create a GATT Server
mDeviceToConnect = device;
final BluetoothManager bluetoothManager = (BluetoothManager) getContext().getSystemService(Context.BLUETOOTH_SERVICE);
try {
DebugLogger.d(TAG, "[Server] Starting Gatt server...");
Logger.v(mLogSession, "[Server] Starting Gatt server...");
openGattServer(getContext(), bluetoothManager);
addImmediateAlertService();
// the BluetoothGattServerCallback#onServiceAdded callback will proceed further operations
} catch (final Exception e) {
// On Nexus 4&7 with Android 4.4 (build KRT16S) sometimes creating Gatt Server fails. There is a Null Pointer Exception thrown from addCharacteristic method.
Logger.e(mLogSession, "[Server] Gatt server failed to start");
Log.e(TAG, "Creating Gatt Server failed", e);
}
} else {
super.connect(device);
}
}
示例13: initBluetooth
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
/**
* enable bluetooth
*/
private void initBluetooth() {
//get Bluetooth service
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
//get Bluetooth Adapter
mBluetoothAdapter = mBluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {//platform not support bluetooth
Log.d(Tag, "Bluetooth is not support");
}
else{
int status = mBluetoothAdapter.getState();
//bluetooth is disabled
if (status == BluetoothAdapter.STATE_OFF) {
// enable bluetooth
mBluetoothAdapter.enable();
}
}
}
示例14: initialize
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
/**
* Initializes bluetooth adapter
*
* @return <code>true</code> if initialization was successful
*/
private boolean initialize() {
// For API level 18 and above, get a reference to BluetoothAdapter through
// BluetoothManager.
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if (bluetoothManager == null) {
loge("Unable to initialize BluetoothManager.");
return false;
}
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
loge("Unable to obtain a BluetoothAdapter.");
return false;
}
return true;
}
示例15: setupBleController
import android.bluetooth.BluetoothManager; //導入依賴的package包/類
boolean setupBleController() {
boolean retvalue = true;
if(bluetoothAdapter == null) {
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
retvalue = false;
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && leScanner == null) {
if(bluetoothAdapter == null) {
retvalue = false;
} else {
leScanner = bluetoothAdapter.getBluetoothLeScanner();
if(leScanner == null)
retvalue = false;
}
}
return retvalue;
}