×

android 蓝牙开发 蓝牙开发 字符

android蓝牙开发,PC端模拟串口接收字符,该如何编程?大小写字母和数字的ascii码值是多少

admin admin 发表于2022-07-15 17:23:53 浏览105 评论0

抢沙发发表评论

android蓝牙开发,PC端模拟串口接收字符,该如何编程


您好,android蓝牙这方面还是很好搞的,因为大家的方式都是差不多的。先说说如何开启蓝牙设备和设置可见时间:
private void search() {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (!adapter.isEnabled()) {
adapter.enable();
}
Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
enable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600); //3600为蓝牙设备可见时间
startActivity(enable);
Intent searchIntent = new Intent(this, ComminuteActivity.class);
startActivity(searchIntent);
}
首先,需要获得一个BluetoothAdapter,可以通过getDefaultAdapter()获得系统默认的蓝牙适配器,当然我们也可以自己指定,但这个真心没有必要,至少我是不需要的。然后我们检查手机的蓝牙是否打开,如果没有,通过enable()方法打开。接着我们再设置手机蓝牙设备的可见,可见时间可以自定义。

完成这些必要的设置后,我们就可以正式开始与蓝牙模块进行通信了:
public class ComminuteActivity extends Activity {
private BluetoothReceiver receiver;
private BluetoothAdapter bluetoothAdapter;
private List《String》 devices;
private List《BluetoothDevice》 deviceList;
private Bluetooth client;
private final String lockName = “BOLUTEK“;
private String message = “000001“;
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_layout);
listView = (ListView) this.findViewById(R.id.list);
deviceList = new ArrayList《BluetoothDevice》();
devices = new ArrayList《String》();
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.startDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
receiver = new BluetoothReceiver();
registerReceiver(receiver, filter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView《?》 parent, View view, int position, long id) {
setContentView(R.layout.connect_layout);
BluetoothDevice device = deviceList.get(position);
client = new Bluetooth(device, handler);
try {
client.connect(message);
} catch (Exception e) {
Log.e(“TAG“, e.toString());
}
}
});
}
@Override
protected void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
private final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Bluetooth.CONNECT_FAILED:
Toast.makeText(ComminuteActivity.this, “连接失败“, Toast.LENGTH_LONG).show();
try {
client.connect(message);
} catch (Exception e) {
Log.e(“TAG“, e.toString());
}
break;
case Bluetooth.CONNECT_SUCCESS:
Toast.makeText(ComminuteActivity.this, “连接成功“, Toast.LENGTH_LONG).show();
break;
case Bluetooth.READ_FAILED:
Toast.makeText(ComminuteActivity.this, “读取失败“, Toast.LENGTH_LONG).show();
break;
case Bluetooth.WRITE_FAILED:
Toast.makeText(ComminuteActivity.this, “写入失败“, Toast.LENGTH_LONG).show();
break;
case Bluetooth.DATA:
Toast.makeText(ComminuteActivity.this, msg.arg1 + ““, Toast.LENGTH_LONG).show();
break;
}
}
};
private class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (isLock(device)) {
devices.add(device.getName());
}
deviceList.add(device);
}
showDevices();
}
}
private boolean isLock(BluetoothDevice device) {
boolean isLockName = (device.getName()).equals(lockName);
boolean isSingleDevice = devices.indexOf(device.getName()) == -1;
return isLockName && isSingleDevice;
}
private void showDevices() {
ArrayAdapter《String》 adapter = new ArrayAdapter《String》(this, android.R.layout.simple_list_item_1,
devices);
listView.setAdapter(adapter);
}
}

大小写字母和数字的ascii码值是多少

A-Z:97-122,A-Z:65-90,0-9:48-57。

ASCII代码使用指定的7或8位二进制组合来表示128或256个可能的字符。

0 ~31和127(总计33个)是控制字符或通信特定字符(其余为可显示字符),例如控制符号:lf(替换),CR(Enter),FF(替换)(替换),DEL(delete)),BS(返回),BEL(戒指)等;交流 - 特定字符:SOH(文本),EOT(文本尾巴),ACK(确认)等;-蓝牙开发

32〜126(总共95个)是字符(32是空格),其中48-57为0至9个阿拉伯数字;

65-90是26个大写字母,97-122是26个小写字母,其余的是标点符号符号,计算符号等。

扩展信息:

ASCII是美国标准,因此它无法满足其他英语国家的需求。

1967年,ISO:国际标准组织)推荐了ASCII的变体。

代码0x40,0x5b,0x5c,0x5d,0x7b,0x7b,0x7c和0x7d“国家储备”,代码0x5E,0x60和0x7E被标记为“当中国需要的特殊字符需要8、9或10个空间,可以用于罐头图形符号”。-字符

在GB2312-1980中,所有国家标准汉字和符号构成94×94矩阵。在此矩阵中,每行称为“区域”,每列称为“位”。每个地区的94个地区(01-94区域)和94位数字(01-94位)。区域代码和位代码的组合形成“位置代码”,这是唯一的汉字或符号。-蓝牙开发

参考信息来源:百度百科全书

标准的ASCII码字符集总共有几个

标准的ASCII代码集总计有128个代码。

标准ASCII代码也称为基本ASCII代码。它使用7个二进制数字(其余1个二进制为0)来表示所有资本和小写字母,数字0至9,标点符号符号和美国英语Character.in中使用的特殊控制:

0 ~31和127(总计33个)是控制字符或特定于通信的字符(其余为可显示字符)。它们没有特定的图形显示,但是根据不同的应用程序,它们会对文本显示产生不同的影响。

32〜126(总共95个)是字符(32是空格),其中48-57是0到9个阿拉伯数字。

65-90是26个大写字母,97-122是26个小写字母,其余的是标点符号符号,计算符号等。

扩展信息:

扩展ASCII代码:

ASCII代码使用指定的7或8位二进制组合来表示128或256个可能的字符,其中128个称为扩展ASCII代码。基于X86的许多系统支持扩展(或“高”)ASCII的使用,并扩展ASCII代码允许每个字符的第八位确定其他128个特殊符号字符,外语字母和图形符号。-字符

参考信息来源:百度百科全书-SCII