本文整理汇总了Java中android.bluetooth.BluetoothAdapter.ACTION_REQUEST_ENABLE属性的典型用法代码示例。如果您正苦于以下问题:Java BluetoothAdapter.ACTION_REQUEST_ENABLE属性的具体用法?Java BluetoothAdapter.ACTION_REQUEST_ENABLE怎么用?Java BluetoothAdapter.ACTION_REQUEST_ENABLE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类android.bluetooth.BluetoothAdapter
的用法示例。
在下文中一共展示了BluetoothAdapter.ACTION_REQUEST_ENABLE属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
listView = (ListView) findViewById(R.id.listview);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1);
listView.setAdapter(mArrayAdapter);
//1.获取BluetoothAdapter;
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
//2.启用蓝牙
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
//查询配对信息
findDevice();
discoverable();
}
} else {
// 不支持蓝牙
Toast.makeText(this, "不支持蓝牙设备", Toast.LENGTH_LONG).show();
}
}
示例2: onResume
@Override
public void onResume() {
super.onResume();
// Register ChipRobotFinder broadcast receiver
this.getActivity().registerReceiver(mChipFinderBroadcastReceiver, ChipRobotFinder.getChipRobotFinderIntentFilter());
// Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled,
// fire an intent to display a dialog asking the user to grant permission to enable it.
if (mBluetoothAdapter != null && !mBluetoothAdapter.isEnabled()) {
if (!mBluetoothAdapter.isEnabled()) {
try {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} catch (ActivityNotFoundException ex) {
}
}
}
// Start scan
ChipRobotFinder.getInstance().clearFoundChipList();
scanLeDevice(false);
updateChipList();
scanLeDevice(true);
}
示例3: startBluetoothServices
/**
* Starts bluetooth and listens to pairing requests and bluetooth state changes
*/
public static void startBluetoothServices() {
mReceiver = new BluetoothReceiver();
//Register the BroadcastReceiver for multiple bluetooth actions
//when another bluetooth device is discovered in range
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
//when discovering action finishes (discovering means that bluetooth searches for nearby devices)
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
//when bluetooth changes from on to off
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
//when another device requests pairing with this device
filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
mainActivity.mainRegisterReceiver(mReceiver, filter);
if(!started) {
//tries to start bluetooth if it is not on
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter!=null){
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
mainActivity.startActivityForResult(enableBtIntent, MainActivity.BLUETOOTH_ON);
} else {
started = true;
}
}
}
}
示例4: enableBluetooth
@ReactMethod
public void enableBluetooth(Callback callback) {
if (getBluetoothAdapter() == null) {
Log.d(LOG_TAG, "No bluetooth support");
callback.invoke("No bluetooth support");
return;
}
if (!getBluetoothAdapter().isEnabled()) {
enableBluetoothCallback = callback;
Intent intentEnable = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
if (getCurrentActivity() == null)
callback.invoke("Current activity not available");
else
getCurrentActivity().startActivityForResult(intentEnable, ENABLE_REQUEST);
} else
callback.invoke();
}
示例5: openBtDevice
private boolean openBtDevice() {
// 获得蓝牙匹配器
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 蓝牙设备不被支持
if (mBluetoothAdapter == null) {
Toast.makeText(getActivity(), "该设备没有蓝牙设备", Toast.LENGTH_LONG).show();
return false;
}
// 蓝牙如果没打开,直接提示需要打开蓝牙
if (!mBluetoothAdapter.isEnabled()) {
// 隐式Intent
Intent enableBtIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUES_BT_ENABLE_CODE); //在onActivityResult有打开服务端线程的操作哦
} else {
// 如果蓝牙在运行本程序前已经人为打开,那么直接启动服务器端线程
Toast.makeText(getActivity(), "蓝牙已经打开! ", Toast.LENGTH_LONG).show();
//暂时不启动服务端线程,因为手机充当的总是客服端
// mAcceptThread = new AcceptThread(mBluetoothAdapter,mHandler); // // // // // // // // // // // // // // // // // // // // // // // // //
// mAcceptThread.start(); // // // // // // // // // // // // // // // //蓝牙提前打开就启动服务端线程? // // // // // //
}
return true;
}
示例6: onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_bluetoothlist);
this.setFinishOnTouchOutside(false);
ButterKnife.bind(this);
mHandler = new Handler();
// indicate scanning in the title
TextView status = (TextView) findViewById(R.id.bluetoothlist_status);
status.setText(R.string.ble_startfind);
// initialize bluetooth manager & adapter
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
// if bluetooth is not currently enabled,
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
else {
// initialize list view adapter
deviceList = new ArrayList();
mLeDeviceListAdapter = new LeDeviceListAdapter(this, R.layout.item_bluetoothdevice);
listView_BluetoothList.setAdapter(mLeDeviceListAdapter);
listView_BluetoothList.setOnItemClickListener(new ListViewItemClickListener());
scanLeDevice(true);
}
}
示例7: init
/**
* Attempts to create the scanner.
*
* @param context
* @return true if successful
*/
public boolean init(final Activity context) {
// New Android M+ permission check requirement.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("This app needs coarse location access");
builder.setMessage("Please grant coarse location access so this app can scan for beacons");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSION_REQUEST_COARSE_LOCATION);
}
});
builder.show();
}
}
BluetoothManager manager = (BluetoothManager) context.getApplicationContext()
.getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter btAdapter = manager.getAdapter();
if (btAdapter == null) {
return false;
} else if (!btAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
context.startActivityForResult(enableBtIntent, mRequestEnableBluetooth);
return false;
} else {
scanner = btAdapter.getBluetoothLeScanner();
}
return true;
}
示例8: onStart
@Override
public void onStart() {
super.onStart();
if(D) Log.e(TAG, "++ ON START ++");
// If BT is not on, request that it be enabled.
// setupChat() will then be called during onActivityResult
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
// Otherwise, setup the chat session
} else {
if (mChatService == null) setupChat();
}
}
示例9: checkBluetoothState
private boolean checkBluetoothState() {
// TODO app freezes after enabling bluetooth through app instead of directly enabling it
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
enableBtIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(enableBtIntent);
Log.i("LOOP_THREAD", "Bluetooth not enabled..");
return false;
}
return true;
}
示例10: init
public void init() {
textViewLog = (TextView) findViewById(R.id.bluetooth_log);
textViewLog.setMovementMethod(new ScrollingMovementMethod());
btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) {
Log.w(TAG, "Bluetooth is not supported");
showToast("Device does not support bluetooth");
finish();
return;
}
Log.i(TAG, "working bluetooth");
if (!btAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_BLUETOOTH);
Log.e(TAG, "Bluetooth not enabled");
return;
}
Log.i(TAG, "Bluetooth enabled");
listPairedDevices.setAdapter(new DeviceAdapter(getApplicationContext(), btAdapter.getBondedDevices()));
listPairedDevices.setOnItemClickListener(itemClickListener);
dbHelper = new TrustChainDBHelper(getApplicationContext());
kp = Key.loadKeys(getApplicationContext());
//start listening for messages via bluetooth
communication = new BluetoothCommunication(dbHelper, kp, this, btAdapter);
communication.start();
}
示例11: enableBluetooth
/**
* Asks to enable bluetooth
*/
@ReactMethod
public void enableBluetooth(final Promise promise) {
try {
if (!checkBluetooth()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
getReactApplicationContext().startActivityForResult(enableBtIntent, NEAR_BLUETOOTH_SETTINGS_CODE, new Bundle());
}
promise.resolve(true);
} catch (Exception e) {
promise.reject("BLE_ACTIVATION_ERROR", e.getMessage());
}
}
示例12: startDiscovery
private void startDiscovery() {
// Check if Bluetooth is enabled. If not, display a dialog requesting user permission to
// enable Bluetooth.
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
final Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
return;
} // else: Bluetooth is enabled
// On Android Marshmallow (6.0) and higher, we need ACCESS_COARSE_LOCATION or
// ACCESS_FINE_LOCATION permission to get scan results, so check if we have. If not, ask the
// user for permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSION_REQUEST_COARSE_LOCATION
);
return;
} // else: permission already granted
} // else: running on older version of Android
// Start scanning
bluetoothAdapter.getBluetoothLeScanner().startScan(scanCallback);
}
示例13: openBluetooth
/**
* 开启蓝牙
*
* @param context
*/
public void openBluetooth(Activity context, int requestCode) {
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
context.startActivityForResult(enableBtIntent, requestCode);
}
}
示例14: onStart
@Override
public void onStart() {
super.onStart();
// If BT is not on, request that it be enabled.
// setupChat() will then be called during onActivityResult
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
// Otherwise, setup the chat session
} else if (mChatService == null) {
setupChat();
}
}
示例15: onStart
@Override
public void onStart() {
super.onStart();
// If BT is not on, request that it be enabled.
// setupChat() will then be called during onActivityResult
if (null == mBluetoothAdapter || !mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
// Otherwise, setup the chat session
} else if (mChatService == null) {
setupChat();
}
}