マサルの備忘録

ハイオク仕様のトルクフルな備忘録

AndroidもLollipopでBLE Peripheralに対応したしやってみたんだけど

スポンサーリンク

結論から言うと。

f:id:domomasarudesu:20150129232651p:plain

Bluetooth LEってなに?

ググってください。

Lollipopって甘いの?

ググってください。むしろ苦いです。

やったこと

Nexus5にAndroid5.0.2を入れて、LEのScanとAdvertiseができるかやってみた。
Scanはできたっぽいけど、BLEデバイス持ってないので動作確認できず(笑)
Advertiseもやってみたんだけど、これが曲者。

mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();

これが常にNULLになってしまう。

BluetoothAdapter | Android Developers

リファレンスには

Returns a BluetoothLeAdvertiser object for Bluetooth LE Advertising operations. Will return null if Bluetooth is turned off or if Bluetooth LE Advertising is not supported on this device.

と書いてあって、Advertiseに対応していない模様・・・
Nexus6買うか・・・

クソコードはこちら

良い子は真似しないでね

package com.masaru.bluetoothlowenergy;

import java.util.List;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView mTextView;
	private BluetoothManager mBluetoothManager;
	private BluetoothAdapter mBluetoothAdapter;
	private BluetoothLeAdvertiser mBluetoothLeAdvertiser;
	private AdvertiseData mAdvertiseData;
	private AdvertiseSettings mAdvertiseSettings;
	private BluetoothLeScanner mBluetoothLeScanner;

	private AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
		String str = "";

		public void onStartFailure(int errorCode) {
			super.onStartFailure(errorCode);

			str += "onStartFailure : " + errorCode + "\n";
			mTextView.setText(str);
		}

		public void onStartSuccess(AdvertiseSettings settingsInEffect) {
			super.onStartSuccess(settingsInEffect);

			str += "onStartSuccess : " + settingsInEffect.toString() + "\n";
			mTextView.setText(str);
		}
	};

	private ScanCallback mScanCallback = new ScanCallback() {
		String str = "";

		public void onBatchScanResults(List<ScanResult> results) {
			super.onBatchScanResults(results);

			for (ScanResult res : results) {
				str += "onBatchScanResults : " + res.getDevice().getName()
						+ "\n";
			}
			mTextView.setText(str);
		}

		public void onScanFailed(int errorCode) {
			super.onScanFailed(errorCode);

			str += "onScanFailed : " + errorCode + "\n";
			mTextView.setText(str);
		}

		public void onScanResult(int callbackType, ScanResult result) {
			super.onScanResult(callbackType, result);

			str += "onScanResult : " + result.getDevice().getName() + "\n";
			mTextView.setText(str);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mBluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
		mBluetoothAdapter = mBluetoothManager.getAdapter();
		mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
		mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();

		AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder();
		dataBuilder.setIncludeTxPowerLevel(false);
		mAdvertiseData = dataBuilder.build();

		AdvertiseSettings.Builder settingBuilder = new AdvertiseSettings.Builder();
		settingBuilder
				.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);
		settingBuilder
				.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED);
		mAdvertiseSettings = settingBuilder.build();
	}

	public void onResume() {
		super.onResume();

		mTextView = (TextView) findViewById(R.id.text_view);
		mBluetoothLeAdvertiser.startAdvertising(mAdvertiseSettings,
				mAdvertiseData, mAdvertiseCallback);
		// mBluetoothLeScanner.startScan(mScanCallback);
	}

	public void onPause() {
		super.onPause();

		mBluetoothLeAdvertiser.stopAdvertising(mAdvertiseCallback);
		// mBluetoothLeScanner.stopScan(mScanCallback);
	}
}

参考にしたサイト

やっぱり無理っぽいっすね。
http://forestlive.sakura.ne.jp/wp/?p=187
http://forestlive.sakura.ne.jp/wp/?p=187