其实前面的除了service大家最关注的的 可能还是 中间设备和外围设备他们之间的连接问题(PS:这也是我最关心的问题,本人曾经在百度知道里面花重财富,结果有人回答,但是他的回答,不过是百度,粘贴复制而已,并没有对我有太多的帮助)
在这里 我也想说 我目前也仅仅是 看到了demo 具体能不能实现传递通信,,还需要 你们的验证(PS:单片机部分 的蓝牙模块,我目前还没有涉及,公司另有负责,我目前做这个只是了解下,已备到时候用的到时候可以不像上次那样,不知所措,一脸迷茫。。(PS:上家公司 的业务是做商城app。。。。跟蓝牙是不是丝毫不搭边。。。。(PS:人生也有涯 ,学而无止境。。。(PS:卧槽 是不是很押韵)))) 好了 废话不多说 ,代码奉上。。。。(再加上一句:有哪位BLE高手懂我上面的问题 不吝赐教,我财富值奉上) 如下: (代码的注释 我有的有道了下。。。原谅我这屌丝想法)
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import com.example.bledemo.BLEService;
import com.example.bledemo.R;
import com.example.bledemo.SampleGattAttributes;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
public class ScanDeviceActivity extends Activity {
private final static String TAG = ScanDeviceActivity.class
.getSimpleName();
public static final String EXTRAS_DEVICE_NAME = "DEVICE_NAME";
public static final String EXTRAS_DEVICE_ADDRESS = "DEVICE_ADDRESS";
private TextView mConnectionState;
private TextView mDataField;
private String mDeviceName;
private String mDeviceAddress;
private ExpandableListView mGattServicesList; //listview 下拉列表
private BLEService mBluetoothLeService;
private ArrayList<ArrayList<BluetoothGattCharacteristic>> mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();
private boolean mConnected = false;
private BluetoothGattCharacteristic mNotifyCharacteristic;
private final String LIST_NAME = "NAME";
private final String LIST_UUID = "UUID";
// Code to manage Service lifecycle.
//代码管理服务生命周期
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName,
IBinder service) {
mBluetoothLeService = ((BLEService.LocalBinder) service)
.getService();
if (!mBluetoothLeService.initialize()) {
Log.e(TAG, "Unable to initialize Bluetooth"); //无法初始化蓝牙
finish();
}
// Automatically