Code example for BluetoothAdapter

Methods: getName, isEnabled, setName

0
 
	public synchronized void relaseInstance() { 
		instance = null;
		restorationBluetoothName(); 
		if ((getBoolean(NFDUtils.BlueToothOpreator))
				&& mBlueTooth != null && mBlueTooth.isEnabled()) {
			mBlueTooth.disable();
			putBoolean(NFDUtils.BlueToothOpreator, false);
		} 
	} 
 
	public boolean init(int type) {
		mDetectNetWorkStatus = true;
		// detectNetWork(); 
		restorationBluetoothName(); 
		mIsOriginalEable = false;
		if ((type & RegistType.BlueTooth.getCode()) == 0) {
			return false; 
		} 
		if (mBlueTooth == null) {
			LogUtil.logOnlyDebuggable(TAG, "此手机不支持蓝牙 ");
			return false; 
		} 
		try { 
			if (mBlueTooth.isEnabled()) {
				mIsOriginalEable = true;
				LogUtil.logOnlyDebuggable(TAG, mBlueTooth.getName());
			} else { 
				if (!mBlueTooth.enable()) {
					return false; 
				} 
				putBoolean(NFDUtils.BlueToothOpreator, true);
			} 
		} catch (Exception e) {
			LogUtil.logOnlyDebuggable(TAG, e.toString());
			return false; 
		} 
 
		return true; 
	} 
 
	public void openBlueTooth() { 
		mBlueTooth = BluetoothAdapter.getDefaultAdapter();
		if (mBlueTooth == null) {
			LogUtil.logOnlyDebuggable(TAG, "此手机不支持蓝牙 ");
			return; 
		} 
		try { 
			if (mBlueTooth.isEnabled()) {
				LogUtil.logOnlyDebuggable(TAG, mBlueTooth.getName());
			} else { 
				if (!mBlueTooth.enable()) {
					return; 
				} 
				putBoolean(NFDUtils.BlueToothOpreator, true);
			} 
		} catch (Exception e) {
			LogUtil.logOnlyDebuggable(TAG, e.toString());
		} 
	} 
 
	public void restorationBluetoothName() { 
		mBlueTooth = BluetoothAdapter.getDefaultAdapter();
		if (mBlueTooth == null) {
			LogUtil.logOnlyDebuggable(TAG, "此手机不支持蓝牙 ");
			return; 
		} 
		if (mBlueTooth.isEnabled()) {
			String blueToothName = mBlueTooth.getName();
			if (blueToothName != null
					&& (blueToothName.startsWith("$") && blueToothName
							.endsWith("^"))) {
				mBlueTooth.setName(getString(NFDUtils.BlueToothKey,
						blueToothName));
			} 
		} 
	}