基于Arduino HC-05蓝牙数据的Android通知功能故障排查求助
Arduino HC-05蓝牙传感器数据Android通知失效排查与修复
核心问题排查方向
针对你的通知功能失效问题,按优先级从以下几个维度逐一排查:
1. Android 8.0+ 通知渠道未配置
Android 8.0(API 26)及以上版本要求所有通知必须绑定到通知渠道,未配置渠道的通知将不会显示。
检查与修复:
在你的NotificationPrompt类中添加渠道创建逻辑,并在初始化时调用:
public class NotificationPrompt { private static final String CHANNEL_ID = "SENSOR_ALERT_CHANNEL"; private final Context context; public NotificationPrompt(Context context) { this.context = context; createNotificationChannel(); // 初始化时创建渠道 } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence channelName = "传感器告警通知"; String channelDesc = "传感器数据超出阈值时推送的通知"; int importance = NotificationManager.IMPORTANCE_HIGH; // 高优先级确保通知弹窗显示 NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance); channel.setDescription(channelDesc); NotificationManager notificationManager = context.getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } } public void showThresholdAlert(String title, String content) { // 构建通知时指定渠道ID NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) // 必须设置纯色小图标,否则通知不显示 .setContentTitle(title) .setContentText(content) .setPriority(NotificationCompat.PRIORITY_HIGH) .setAutoCancel(true); NotificationManagerCompat.from(context).notify(1, builder.build()); } }
2. 通知权限未获取
Android 13(API 33)及以上版本新增POST_NOTIFICATIONS权限,必须动态申请后才能发送通知。
检查与修复:
- 在
AndroidManifest.xml中添加权限声明:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
- 在
TerminalFragment的初始化逻辑中动态申请权限:
@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // 申请通知权限(仅针对Android 13+) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, 1001); } } }
3. 传感器数据解析错误,阈值判断未触发
如果Arduino发送的数据格式与Android端解析逻辑不匹配,会导致阈值判断代码根本不会执行。
检查与修复:
- 确认Arduino发送的格式(例如固定用逗号分隔:
temp:28.5,humi:62,lpg:45,light:350) - 在
TerminalFragment的数据接收回调中添加异常捕获,避免解析失败导致逻辑中断:
// 假设数据接收回调方法 private void processReceivedData(String rawData) { // 去除换行符等冗余字符 rawData = rawData.trim(); String[] dataPairs = rawData.split(","); for (String pair : dataPairs) { String[] keyValue = pair.split(":"); if (keyValue.length != 2) continue; // 跳过格式错误的数据段 String key = keyValue[0].trim(); String valueStr = keyValue[1].trim(); try { switch (key) { case "temp": double temp = Double.parseDouble(valueStr); if (temp > 30.0) { // 假设温度阈值30℃ new NotificationPrompt(requireContext()) .showThresholdAlert("温度超标", "当前温度:" + temp + "℃"); } break; case "lpg": int lpg = Integer.parseInt(valueStr); if (lpg > 50) { // 假设LPG浓度阈值50 new NotificationPrompt(requireContext()) .showThresholdAlert("燃气浓度超标", "当前LPG浓度:" + lpg); } break; // 其他传感器类似处理 } } catch (NumberFormatException e) { Log.e("TerminalFragment", "解析传感器数据失败:" + e.getMessage()); } } }
4. NotificationPrompt上下文传递错误
如果创建NotificationPrompt时传入的上下文无效(例如getActivity()返回null),会导致通知无法构建。
修复:
在Fragment中使用requireContext()获取有效上下文,而非getActivity():
// 错误写法 new NotificationPrompt(getActivity()).showThresholdAlert(...) // 正确写法 new NotificationPrompt(requireContext()).showThresholdAlert(...)
5. 通知小图标不符合要求
Android系统要求通知小图标必须是纯色透明背景的矢量图,若使用彩色或非透明图标,通知可能无法显示。
检查:
确保res/drawable下的ic_notification是符合要求的图标,可替换为系统默认图标测试:
.setSmallIcon(android.R.drawable.stat_notify_error)
内容的提问来源于stack exchange,提问作者AdhamMK
相关产品推荐
相关产品推荐

