下面列出了android.bluetooth.BluetoothAdapter#getScanMode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 他の機器から探索可能になるように要求する
* bluetoothに対応していないか無効になっている時はIllegalStateException例外を投げる
* @param activity
* @param duration 探索可能時間[秒]
* @return 既に探索可能であればtrue
* @throws IllegalStateException
*/
public static boolean requestDiscoverable(@NonNull final Activity activity, final int duration) throws IllegalStateException {
final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if ((adapter == null) || !adapter.isEnabled()) {
throw new IllegalStateException("bluetoothに対応していないか無効になっている");
}
if (adapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
final Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, duration);
activity.startActivity(intent);
}
return adapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
}
/**
* 他の機器から探索可能になるように要求する
* bluetoothに対応していないか無効になっている時はIllegalStateException例外を投げる
* @param fragment
* @param duration 0以下ならデフォルトの探索可能時間で120秒、 最大300秒まで設定できる
* @return
* @throws IllegalStateException
*/
public static boolean requestDiscoverable(@NonNull final Fragment fragment, final int duration) throws IllegalStateException {
final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if ((adapter == null) || !adapter.isEnabled()) {
throw new IllegalStateException("bluetoothに対応していないか無効になっている");
}
if (adapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
final Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
if ((duration > 0) && (duration <= 300)) {
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, duration);
}
fragment.startActivity(intent);
}
return adapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
}
public void setDiscoverable() {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
return;
}
// change name if necessary
String originalName = adapter.getName();
if (originalName.lastIndexOf(_deviceNamePostFix) != originalName.length() - _deviceNamePostFix.length()) {
if (_connectionHelperListener != null) {
_connectionHelperListener.onAdapterNameChange(originalName);
}
Log.d(TAG, "setDiscoverable: Bluetooth device name set to " + (originalName + _deviceNamePostFix));
adapter.setName(originalName + _deviceNamePostFix);
}
// set discoverable if necessary
if (adapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
_activity.startActivity(discoverableIntent);
}
}
private void setDiscoverable() {
Log.d(TAG, "setDiscoverable: ");
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
return;
}
// change name if necessary
String originalName = adapter.getName();
if (originalName.lastIndexOf(GoogleFlipGameApplication.DEVICE_POSTFIX) != originalName.length() - GoogleFlipGameApplication.DEVICE_POSTFIX.length()) {
GoogleFlipGameApplication.setOriginalBluetoothDeviceName(originalName);
Log.d(TAG, "setDiscoverable: Bluetooth device name set to " + (originalName + GoogleFlipGameApplication.DEVICE_POSTFIX));
adapter.setName(originalName + GoogleFlipGameApplication.DEVICE_POSTFIX);
}
// set discoverable if necessary
if (adapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Log.d(TAG, "setDiscoverable: scan mode is not discoverable, starting activity with code " + ActivityRequestCode.REQUEST_ENABLE_SCAN);
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 120);
getActivity().startActivityForResult(discoverableIntent, ActivityRequestCode.REQUEST_ENABLE_SCAN);
}
}
public static boolean isWorking() {
BluetoothAdapter adapter = BluetoothUtil.getBluetoothAdapter(RumbleApplication.getContext());
if ( adapter == null)
return false;
return ((adapter.getScanMode() == BluetoothAdapter.STATE_CONNECTING) ||
(adapter.getScanMode() == BluetoothAdapter.STATE_DISCONNECTING) );
}
private void resetTitle ()
{
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter.getScanMode() ==
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
setTitle(getString(R.string.app_name)
+ " @" + mLocalAddress);
}
else
setTitle(getString(R.string.app_name) + ": " + getString(R.string.listen_mode));
}
public static boolean isDiscoverable() {
BluetoothAdapter adapter = BluetoothUtil.getBluetoothAdapter(RumbleApplication.getContext());
if ( adapter == null)
return false;
return (adapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
}