一.添加权限
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE">
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE">
<uses-permission android:name="android.permission.INTERNET" /> <!-- 管理蓝牙设备的权限 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- 在sd卡中创建与删除文件权限 -->
android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" /> <!-- 向SD卡写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
android:name="android.permission.CLEAR_APP_CACHE"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
二 获取本地蓝牙名称 搜索蓝牙设备
/**
* 开始扫描蓝牙
*/
private void startscan(){
Intent enabler =new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivity(enabler);
list.clear();
if (adapter !=null) {
adapter.notifyDataSetChanged();
// bluemessage.setText("");
listdevice.clear();
}
/**
* 开启蓝牙服务端
*/
ThreadPoolProxyFactory.getNormalThreadPoolProxy().execute(new Runnable() {
@Override
public void run(){
BltService.getInstance().startBluService();
}
});
Log.d("开始扫描", "开始扫描了");
Acp.getInstance(this).request(new AcpOptions.Builder()
.setPermissions(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
.build(),
new AcpListener(){
@Override
public void onGranted(){
Log.d("来到这里了", "来到这里了......");
if (bluetoothadapter.isDiscovering()){
bluetoothadapter.cancelDiscovery();
}
bluetoothadapter.startDiscovery();
}
@Override
public void onDenied(List permissions){
}
});
}
/**
* 判断蓝牙是否开启
*
* @return
*/
public boolean blueisenable() {
if (bluetoothadapter.isEnabled()) {
return true;
}else {
return false;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode ==RESULT_OK) {
if (requestCode ==1) {
startscan();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(blueToothReceiver);
EventBus.getDefault().unregister(this);
}
控制打印机语法
PrintService.pl.write(new byte[]{0x1B, 0x61, 0x01});//指令 文字中间对齐
PrintService.pl.write("*****饿了么*****".getBytes("GBK")); //打印文字
PrintService.pl.printText("\n"); //换行
PrintService.pl.write(new byte[]{0x1d, 0x21, 0x00}); //字体大小
//打印时间
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//获取当前时间
Date date =new Date(System.currentTimeMillis());
PrintService.pl.write("下单时间:".getBytes("GBK"));
PrintService.pl.write(simpleDateFormat.format(date).getBytes("GBK"));