本文的目的是介绍AndroidBLEonCharacteristicRead和onCharacteristicChanged从未调用过的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也
本文的目的是介绍Android BLE onCharacteristicRead和onCharacteristicChanged从未调用过的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于(React) useDispatch TypeError: Object(...) is not a function handleChange、159. Longest Substring With At Most Two Distinct Characters、ACTION_DISCOVERY_STARTED从未调用过android、android 4.3蓝牙ble不要调用onCharacteristicRead()的知识。
本文目录一览:- Android BLE onCharacteristicRead和onCharacteristicChanged从未调用过
- (React) useDispatch TypeError: Object(...) is not a function handleChange
- 159. Longest Substring With At Most Two Distinct Characters
- ACTION_DISCOVERY_STARTED从未调用过android
- android 4.3蓝牙ble不要调用onCharacteristicRead()
Android BLE onCharacteristicRead和onCharacteristicChanged从未调用过
我在Play商店使用了一个名为BLE Scanner的非常方便的小程序来帮助我向我提供有关该设备及其服务和特性的更多信息.
这就是为什么我只是硬编码服务2,特征0.我只是无法弄清楚为什么在我写writeDescript后,我从来没有看到任何回来.有趣的是,我可以使用其他一些特性(一个是温度间隔),我确实收到了一个响应(虽然数据是乱码.)
另外,出于好奇,为什么这个特征有两个描述符?
此代码包含在我的MainActivity方法中.不确定这是否会有所作为.我已经看过并尝试了几种方法,但没有运气.
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt,int status,int newState) { ... } @Override public void onServicesdiscovered(BluetoothGatt gatt,int status) { mGatt = gatt; List<BluetoothGattService> services = mGatt.getServices(); Log.i("onServicesdiscovered",services.toString()); BluetoothGattCharacteristic characteristic = services.get(2).getcharacteristics().get(0); mGatt.setCharacteristicNotification(characteristic,true); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHaraCTERISTIC_CONfig)); descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); mGatt.writeDescriptor(descriptor); } @Override public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic,int status) { ... } @Override public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) { ... } };
更新:
我决定检查onDescriptorWrite方法并记录一些信息.
@Override public void onDescriptorWrite(BluetoothGatt gatt,BluetoothGattDescriptor descriptor,int status) { Log.i("descriptorWRITE",Integer.toString(status)); }
有趣的是,状态返回13,即“写入操作超过属性的最大长度”.
我将进一步研究这个问题.
解决方法
(React) useDispatch TypeError: Object(...) is not a function handleChange
如何解决(React) useDispatch TypeError: Object(...) is not a function handleChange?
我正在做一个简单的 React 应用程序,其中任何用户在输入文本中引入一个文本,它会自动更新商店中的状态。 这是 React 的非常简单的实践,但我对这个错误感到非常挣扎和沮丧,因为我尝试了所有方法,但仍然遇到同样的问题。
我正在使用“react-redux”和“@reduxjs/toolkit”依赖项。我尝试了所有方法,检查并比较了类似问题的类似示例和解决方案,我从头开始重新制作了该应用程序,以确保不同版本的依赖项没有任何问题,并且我尝试使我的代码非常简单它有效,但无效。
我希望你们中的任何人都可以给我建议或解决方案。 谢谢!!
错误
TypeError: Object(...) is not a function
handleChange
C:/Users/Ruben/Desktop/Projects/React/reddit2/reddit/src/features/search/SearchBar.js:14
11 |
12 |
13 | const handleChange = (e) => {
> 14 | dispatch(setSearchTerm(e.target.value))
| ^ 15 | }
16 |
17 |
View compiled
▶ 19 stack frames were collapsed.
This screen is visible only in development. It will not appear if the app crashes in production.
Open your browser’s developer console to further inspect this error. Click the ''X'' or hit ESC to dismiss this message.
这些是文件:
SearchBar 组件
import React from "react";
import {usedispatch,useSelector} from "react-redux";
import {setSearchTerm,selectSearchTerm} from "../search/searchSlice";
export const SearchBar = () => {
const dispatch = usedispatch();
const term = useSelector(selectSearchTerm);
const handleChange = (e) => {
dispatch(setSearchTerm(e.target.value))
}
return(
<div>
<input
id="search"
type="text"
value={term}
onChange={handleChange}
placeholder="Introduce your Topic"
/>
</div>
);
};
searchSlice
import {createSlice} from "@reduxjs/toolkit";
export const searchSlice = createSlice({
name: ''search'',initialState: '''',reducer: {
setSearchTerm: (state,action) => {state.search = action.payload},}
});
export const {setSearchTerm} = searchSlice.actions;
export const selectSearchTerm = (state) => state.search;
export default searchSlice.reducer;
商店
import {configureStore} from "@reduxjs/toolkit";
import searchReducer from "../features/search/searchSlice";
export default configureStore({
reducer: {
search: searchReducer,},});
索引
import React from ''react'';
import ReactDOM from ''react-dom'';
import { Provider } from "react-redux";
import ''./index.css'';
import App from ''../src/App/App'';
import reportWebVitals from ''./reportWebVitals'';
import store from "../src/App/store";
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,document.getElementById(''root'')
);
Package.json
{
"name": "reddit","version": "0.1.0","private": true,"dependencies": {
"@reduxjs/toolkit": "^1.5.1","@testing-library/jest-dom": "^5.13.0","@testing-library/react": "^11.2.7","@testing-library/user-event": "^12.8.3","react": "^17.0.2","react-dom": "^17.0.2","react-redux": "^7.2.4","react-scripts": "4.0.3","web-vitals": "^1.1.2"
},
解决方法
问题
- 您在
createSlice
中有一个拼写错误,正确的键是reducers
,带有“s”,而不是reducer
。这导致切片操作实际上未定义。 - 您的状态不太正确。它不是一个可起草的对象。
解决方案
更正reducer key并提供正确的状态。
export const searchSlice = createSlice({
name: "search",initialState: { search: "" },// <-- object state
reducers: {
setSearchTerm: (state,action) => {
state.search = action.payload; // <-- update property
}
}
});
export const { setSearchTerm } = searchSlice.actions;
export const selectSearchTerm = (state) => state.search.search; // <-- select property
export default searchSlice.reducer;
159. Longest Substring With At Most Two Distinct Characters
题目:
Given a string, find the length of the longest substring T that contains at most 2 distinct characters.
For example, Given s = “eceba”,
T is "ece" which its length is 3.
解法:
//最重要的是把最后一次出现的这个char的index记在hashmap的value里面。所以当出现不止两个distinc的数的时候,把这个value最低的char删掉,把最lo的index加1就可以啦
public int lengthOfLongestSubstringTwoDistinct(String s) {
if (s.length() < 1) return 0;
Map<Character, Integer> map = new HashMap<Character, Integer>();
int lo = 0;
int hi = 0;
int maxLength = 0;
for (int i = 0; i < s.length(); i++) {
if (map.size() <= 2) {
char c = s.charAt(i);
map.put(c, i);
hi++;
}
if (map.size() > 2) {
int leftMost = s.length();
for (int value : map.values()) {
leftMost = Math.min(leftMost, value);
}
lo = leftMost + 1;
char c = s.charAt(leftMost);
map.remove(c);
}
maxLength = Math.max(maxLength, hi - lo);
}
return maxLength;
}
ACTION_DISCOVERY_STARTED从未调用过android
public class AddPrinter extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); bluetoothAdapter.startdiscovery(); filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_disCOVERY_STARTED); filter.addAction(BluetoothAdapter.ACTION_disCOVERY_FINISHED); registerReceiver(receiver,filter); } broadcastReceiver receiver = new broadcastReceiver() { @Override public void onReceive(Context context,Intent intent) { String action = intent.getAction(); System.out.println(action); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); adapter.add(device); } else if (BluetoothAdapter.ACTION_disCOVERY_STARTED.equals(intent.getAction())) { System.out.println("STARTED"); } else if (BluetoothAdapter.ACTION_disCOVERY_FINISHED.equals(intent.getAction())) { Utils.dialog.dismiss(); } } };
我遇到的问题是如此奇怪,由于某种原因,从未调用过ACTION_disCOVERY_STARTED,但所有其他操作都没问题,我缺少什么?感谢您的时间.
解决方法
public class AddPrinter extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(receiver,filter); filter = new IntentFilter(BluetoothAdapter.ACTION_disCOVERY_STARTED); registerReceiver(receiver,filter); filter = new IntentFilter(BluetoothAdapter.ACTION_disCOVERY_FINISHED); registerReceiver(receiver,filter); // bluetoothAdapter.startdiscovery(); }
把bluetoothAdapter.startdiscovery();注册接收后
android 4.3蓝牙ble不要调用onCharacteristicRead()
我已经将通知设置为android,它不是在调用方法onCharacteristicRead()
???? 它不进入该功能。为什么会这样呢?
任何帮助表示赞赏
要求解决方案。
这是我的代码:
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { Log.i(TAG, "Connected to GATT server."); // Attempts to discover services after successful connection. Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { Log.i(TAG, "Disconnected from GATT server."); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { gattServices = mBluetoothGatt .getService(SampleGattAttributes.SERVICES_UUID); if (gattServices != null) { gattCharacteristics = gattServices .getCharacteristic(SampleGattAttributes.CHARACTERISTIC_UUID); System.out.println("character-->" + gattCharacteristics); } if (gattCharacteristics != null) { System.out.println("Characteristic not null"); System.out.println("Characteristic Properties-->" + gattCharacteristics.getProperties()); mBluetoothGatt.setCharacteristicNotification(gattCharacteristics, true); } } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { System.out.println("in read"); if (status == BluetoothGatt.GATT_SUCCESS) { byte[] data = characteristic.getValue(); System.out.println("reading"); System.out.println(new String(data)); } } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { // System.out.println("change"); byte[] data = characteristic.getValue(); System.out.println(new String(data)); }};
先感谢您!!
答案1
小编典典首先,onCharacteristicRead
如果您通过以下方式阅读了特征,将触发:
mBluetoothGatt.readCharacteristic(characteristic);
读取特征和设置通知是两件事。您要从中获取数据的特征的类型是什么?
是吗:
- 读
- 通知
- 表明
如果是read
,则可以使用mBluetoothGatt.readCharacteristic(characteristic);
方法读取特征,但如果是notify
或indicate
首先,则必须descriptor
通过调用以下方法来读取特征:
mBluetoothGatt.readDescriptor(ccc);
读取后,它将通过调用onDescriptorRead
回调返回数据。
在这里,您可以通过以下方式通过通知或指示来设置(订阅)特征:
mBluetoothGatt.setCharacteristicNotification(characteristic, true)
一旦返回,true
您将需要再次写入描述符(通知或指示的值)
BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(CCC);clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);// or//clientConfig.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);mBluetoothGatt.writeDescriptor(clientConfig);
完成此操作后,onCharacteristicChanged
每次特性更改时,您都会通过回调获得通知。
您可以在此处阅读更多有关Android上的蓝牙连接
以及在此处了解蓝牙特性的信息
关于Android BLE onCharacteristicRead和onCharacteristicChanged从未调用过的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于(React) useDispatch TypeError: Object(...) is not a function handleChange、159. Longest Substring With At Most Two Distinct Characters、ACTION_DISCOVERY_STARTED从未调用过android、android 4.3蓝牙ble不要调用onCharacteristicRead()的相关知识,请在本站寻找。
本文标签: