月度归档:2015年09月

自己写的一个Android BLE 工具

apk下载地址

http://fir.im/bleYan

screenshot

为了验证蓝牙库(轻松搞定Android蓝牙4.0(Bluetooth Low Energy))是否好用,也是拼了。

支持的功能

  • Android 5.0风格
  • 扫描蓝牙设备
  • 可以写蓝牙命令
  • 可以开启Notify
  • 可以读取characteristic特征值
  • Notify接收的数据可以Share
  • 蓝牙设备可收藏和设置别名
  • 蓝牙命令可以管理

todo 列表

  • 蓝牙日志输出

缺陷

  • 多个Activity跳转,蓝牙回调没有共享,导致连接不能共享(有好的方案了再来实现)

源码地址

GitHub地址:https://github.com/captain-miao/bleYan

轻松搞定Android蓝牙4.0(Bluetooth Low Energy)

基础知识参考:Android 蓝牙4.0(1)

Android 蓝牙3大操作

  • 扫描蓝牙设备
  • 连接蓝牙设备
  • 蓝牙蓝牙通信

一、扫描蓝牙设备

蓝牙扫描Android4.x方式:startLeScan (LeScanCallback)
蓝牙扫描Android5.x方式:startScan (ScanCallback callback)
简化扫描操作:像调用View.OnClickListener一样简单

    mBleScanner = new BleScanner(activity, new SimpleScanCallback() {
        @Override
        public void onBleScan(BluetoothDevice bluetoothDevice, int rssi, byte[] scanRecord) {
           //device :设备信息 mac、name
           //rssi :可以认为是蓝牙信号值
           //scanRecord :广播报文
           doSomething();
        }

        @Override
        public void onBleScanFailed(BleScanState scanState) {
           //scanState 扫描错误码
        }
    });
    //开始扫描(一直扫描)
    mBleScanner.startBleScan();
    //开始扫描并设置扫描时长(超时回调onBleScanFailed())
    mBleScanner.onStartBleScan(long timeoutMillis);
    //停止扫描
    mBleScanneronStopBleScan();

二、连接蓝牙设备

1.扫描设备成功后,bluetoothDevice可以直接连接设备(如果不需要后续操作的话)
  BluetoothDevice.connectGatt(Context context, boolean autoConnect, new BluetoothGattCallback() {
      @Override
      public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
          BleLog.i(TAG, "onConnectionStateChange: State = " + BleUtils.getBleConnectStatus(status)
                  + " newState = " + BleUtils.getBleConnectStatus(newState));

          if (newState == BluetoothProfile.STATE_CONNECTED) {
              //连接成功
          } else {
              //连接成功失败

          }
      }
  });
2.扫描设备成功后,连接设备需要后续的操作,通过下面方式)
  mBleHelper = new AppBluetoothHelper(Context context, BleCallback bleCallback);
  mBleHelper.bindService(new AppBluetoothHelper.OnBindListener() {
      @Override
      public void onServiceConnected() {
          connectDevice(String deviceMac);
      }
  });
  public void connectDevice(final String deviceMac) {
    mBleHelper.connectDevice(deviceMac, new ConnectCallback() {
        @Override
        public void onConnectSuccess() {
            mBleHelper.mConnCallback = null;//不再需要回调了
            
        }

        @Override
        public void onConnectFailed(ConnectError error) {

        }
    });
  }

三、蓝牙设备通信

依然像调用View.OnClickListener一样简单,发送消息之后响应回调回来
目前只实现了常用的三个操作

    //写~
    mBleHelper.writeCharacteristic(UUID serviceUUID, UUID CharacteristicUUID, byte[] values)
    //开关notify
    mBleHelper.updateCharacteristicNotification(UUID serviceUUID, UUID characteristicUUID, UUID descriptorUUID, boolean enable)
    //读取特征值
    mBleHelper.readFromCharacteristic(UUID serviceUUID, UUID CharacteristicUUID) 
    //bleCallback中接收蓝牙响应~可以通过uuid来区分消息
    BluetoothHelper(Context context, BleCallback bleCallback)
    //Callback triggered as a result of a remote characteristic notification.
    //BluetoothGattCallback#onCharacteristicChanged --> onCharacteristicNotification
    public void onCharacteristicNotification(UUID uuid, byte[] data) {

    }

    //Callback reporting the result of a characteristic read operation.
    //BluetoothGattCallback#onCharacteristicChanged
    public void onCharacteristicRead(UUID uuid, byte[] data) {

    }

    //Callback indicating the result of a characteristic write operation.
    //BluetoothGattCallback#onCharacteristicWrite
    public void onCharacteristicWrite(UUID uuid, int status) {

    }

搞定收工

GitHub地址:https://github.com/captain-miao/bleYan
apk下载地址:http://fir.im/bleYan

Android 4.4 实现透明状态栏Translucent system bar(Status bar)

Android 5.0的Material Design酷炫、吊炸天,先来解决黑黑的手机状态栏,再一步一步实现Material Design。
ThemeColors
对于Android 5.0 Status bar和App bar的背景色可以轻松定义,对于Android 4.4如何实现呢?
来两张效果图,那张更清晰、好看?
android_screenshot_default_system_bar_mark《—–》android_screenshot_translucent

Status bar

On Android, the status bar contains notification icons and system icons.
Android status bar height: 24dp(mi4 手机不是这个值-_-#)

ps:Android navigation bar 会引发一些问题,暂时不写了。

预备基础知识(Translucent system bars)

android:fitsSystemWindows

布局时是否考虑给系统窗口留出空间,true为留出空间(如 Status Bar and App bar)

android:clipToPadding

是否允许ViewGroup在padding中绘制,该值默认为true,即允许,padding无效;false:padding生效.

透明系统栏设置(Status bar 和 Navigation bar)
Theme.Holo.NoActionBar.TranslucentDecor
Theme.Holo.Light.NoActionBar.TranslucentDecor

如果设置了透明系统栏就必须设置fitsSystemWindows:true和android:clipToPadding:false(适用于listview等)留出系统栏空间而不会被遮挡.
也可以在自定义主题中,增加windowTranslucentNavigation和windowTranslucentStatus属性。

Activity,Window和View之间的关系
        //了解 下面方法是获取 rootView
        Window win = activity.getWindow();
        ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();

DecorView

SystemBarTint 项目

Apply background tinting to the Android system UI when using KitKat translucent modes.

//只对4.4做修改~修改代码:Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT){
}

设置4.4的Status bar颜色

第一步:定义主题Theme

values/
values-v19/


<style name="AppNavigationDrawerTheme" parent="@android:style/Theme.Holo.Light">
        <!--Translucent system bars-->
        <item name="android:windowTranslucentNavigation">false</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

values-v21/


<style name="AppNavigationDrawerTheme" parent="@android:style/Theme.Holo.Light">
        <!--Translucent system bars-->
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:windowTranslucentStatus">false</item>
    </style>

第二步:设置布局属性

//同样只对values-v19/设置

    android:fitsSystemWindows="@bool/fitsSystemWindows"
    android:clipToPadding="false"
第三步:设置颜色值

//同样只对Api19生效
//Activity

	@TargetApi(19)
	public static void setTranslucentStatus(Activity activity, boolean on) {
	if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
		SystemBarTintManager tintManager = new SystemBarTintManager(activity);
		tintManager.setStatusBarTintEnabled(true);
		tintManager.setNavigationBarTintEnabled(false);
		tintManager.setStatusBarTintColor(activity.getResources().getColor(R.color.colorPrimary));
	}
}

//ActionBarActivity

    @TargetApi(19)
    public void setTranslucentStatus(Activity activity, boolean on) {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
        Window win = activity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);

        SystemBarTintManager tintManager = new SystemBarTintManager(activity);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setNavigationBarTintEnabled(true);
        tintManager.setStatusBarTintColor(activity.getResources().getColor(R.color.colorPrimary));
        tintManager.setNavigationBarTintColor(activity.getResources().getColor(R.color.colorPrimary));
        tintManager.setStatusBarTintResource(R.color.colorPrimary);
    }
}

搞定收工

GitHub地址:https://github.com/captain-miao/AndroidStartupDemo
apk下载地址:http://fir.im/yan

遇到的问题